0

I'm trying to add markup from my Typescript file. Here's what I'm trying:

textString = `<b>This</b> should be bold`;
parser = new DOMParser();
parsedHTML = this.parser.parseFromString(this.textString, 'text/html');

and I'm spitting it back out with {{parsedHTML}} in HTML side but all I'm getting back is [object HTMLDocument].


How would I display a formatted string in an *ngFor loop?

Typescript

textString = `<b>This</b> should be bold`;

  planBundle = [
    'One month ($4.99)',
    `One year ($47.90 - 20% Discount)`,
    this.textString
  ];

HTML

<div>
  <mat-radio-group layout="vertical" [(ngModel)]="selectedPlan">
    <mat-radio-button *ngFor="let plan of planBundle" [value]="plan" style="display: block; margin-top: 8px;">
      {{plan}}
    </mat-radio-button>
  </mat-radio-group>
</div>

Current Output

enter image description here


Could you add inline CSS into this? It doesn't seem to be showing up. Here's what I'm trying:

Typescript

 planYearTextString = `One year payment <s>($59.88)</s> <b>$47.90</b> <span style="color: green;">(Save 20%)</span>`;

  planBundle = [
    'One month ($4.99)',
    this.planYearTextString
  ];

Output

enter image description here

4
  • parsedHTML.body.innerHTML Commented Jan 2, 2018 at 9:16
  • That finally displays my content, thanks. But I'm getting the unformatted text <b>This</b> should be bold. How can I format this properly? Commented Jan 2, 2018 at 9:22
  • 1
    <div [innerHtml]="yourVariable"></div> Commented Jan 2, 2018 at 9:53
  • Actually, do you know how I would do this inside an *ngFor loop? I edited my question above. Commented Jan 2, 2018 at 10:34

2 Answers 2

3

Same principle, you need to use innerHtml

<div>
  <mat-radio-group layout="vertical" [(ngModel)]="selectedPlan">
    <mat-radio-button *ngFor="let plan of planBundle" [value]="plan" 
style="display: block; margin-top: 8px;">
     <span [innerHtml]="plan"></span>
    </mat-radio-button>
  </mat-radio-group>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Could you also add some inline CSS into this? I modified my question again :)
Sorry I don't know much about css. Anyway, people here will help you if you are stuck with something, but not do everything for you. Especially with css, where "nice" is not an accurate requirement (I could change these buttons to bright pink oval shapes, but you light not find it 'nice')
0

As per David's comment, the solution to get formatted text into the template is:

<div [innerHtml]="textString"></div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.