1

I iterate through an array with ngFor. The array contains strings like this: "Some Text 1 <br> some text 2". Here is the HTML:

<div class="content" *ngFor="let item of graphService.contents">{{item}}</div>

It prints the correct text but doesn't break the line. It prints the br like a normal word but I want it to break the line. How can I achieve that without splitting the string in two like "Array.firstString + <br> + Array.secondString?

MMF
  • 33
  • 7
  • If your item contains html then you have to use https://angular.io/api/platform-browser/DomSanitizer – Michael Aug 19 '19 at 11:59

2 Answers2

1

To prevent escaping HTML u can do :

    <div class="content" 
         *ngFor="let item of graphService.contents"
         [innerHTML]="item"></div>
lovis91
  • 1,988
  • 2
  • 14
  • 24
0

You can use this

<div class="content" *ngFor="let item of graphService.contents">
    <div [innerHTML]="item"></div>
</div>

If you want to render a plain text from rich text then you can add this function.

this.utilService.htmlToPlaintext(content);

Define a function htmlToPlaintext() like this in your service.

htmlToPlaintext(text) {
    var plain = text ? String(text).replace(/\\t/g, '') : '';
    return text ? String(plain).replace(/\\n/g, '') : '';
  }
Prashanth Damam
  • 841
  • 8
  • 25