5

I've tried to render a raw HTML using innerHTML, as bellow:

 <span *ngIf="displacyHTML " [innerHTML]="displacyHTML"></span>

This HTML has style in line, but it does not work in that way.

The HTML is rendered, but the style does not.

If I paste the same raw HTML into a separate file it works perfectly.

The styles I mention is used basically to change the background color of the mark tags.

3
  • Please include in the question an example of "raw HTML" that does not work. Commented Jan 2, 2020 at 19:01
  • Does this answer your question? style not working for innerhtml in Angular 2 Typescript Commented Jan 2, 2020 at 19:34
  • I solve the problem reloading the component with the new html + css. Apparently the innerHTML does not load the CSS with the HTML... I do not know exactly why. Commented Jan 2, 2020 at 20:20

1 Answer 1

6

Potentially, you need a SafePipe for your html as your browser does not trust injected html code:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'safePipe'})

export class safePipe implements PipeTransform  {

constructor(protected sanitizer: DomSanitizer):{}

  transform(value) {
    return this.sanitizer.bypassSecurityTrustHtml(value);
  }
}

usage in HTML:

<span [innerHtml]="potentiallyNotSafeHtmlCode | safePipe"></span>
Sign up to request clarification or add additional context in comments.

1 Comment

Why "potentially"? When don't you need this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.