Convert text(including style) getting in API to HTML in Angular 8

Viewed 154

I want to convert text received in API response to the html file with style.

my response:-

description: "<span style="color:green">SAVE Rs.150 </span> on your 1st Booking- Applicable for Weekdays' Slots"

I have tried this but not working. style is not applying.

<div [innerHtml]="coupon?.description"">
</div>
1 Answers

You have to sanitize HTML string.

ts:

constructor(private domSanitizer: DomSanitizer) { }

sanitize(): SafeHtml {
  return this.domSanitizer.bypassSecurityTrustHtml(this.coupon?.description);
}

html:

<div [innerHtml]="sanitize()">
</div>
Related