1

I am building an application where I need to inject some html from the .ts file to the .html file in Angular. I am using the following code for that.

.ts

..
export class ConclusionPageComponent implements OnInit {
  ..
  pageContent: SafeHtml;
  ..
  ngOnInit() {
    ..
    this.pageContent = this.sanitizer.bypassSecurityTrustHtml(data.data.step_data.data.content);
    ..
  }
}

.html

<div class="container-fluid ">
    <div class="row">
        <div class="col" [innerHtml]="pageContent">
        </div>
    </div>
</div>

.scss

.emphasis {
    font-style: italics;
    font-weight: bold;
}

There are some there are some html elements in pageContent with the class .emphasis, but the style in the .emphasis class is not getting applied. Actually, even the class is not getting applied to the elements (I've checked via inspect element.) I am also using some Angular material elements in pageContent and the material style is not getting applied to these elements. If I keep an Angular material element in the .html file itself, the Angular material styles are getting applied to it.

So, the main problem is that the css classes are not getting applied to the innerHtml I am injecting from .ts. How to fix this problem?

Just marking the html safe doesn't help. Setting ViewEncapsulation.None still doesn't help with the Angular material styles.

Edit: Stackblitz of a simpler version of the problem.

Outsider
  • 1,166
  • 1
  • 14
  • 27

0 Answers0