0

I've this...

const htmlVar = "<html>
<div style="display:flex">
<div>
  <h1>Hello World</h1>
</div>
</div>
";

and on my html I've this..

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

It's render everything, but the style :(

I attempted the solution of this link: style not working for innerhtml in Angular 2 Typescript

but it's not working. Maybe because it's too old.

Any clue ?

3
  • 2
    Consider using the DomSanitizer to get this to work. Commented Aug 15, 2019 at 13:46
  • 2
    A div can't contain a head or body, that's invalid HTML Commented Aug 15, 2019 at 13:49
  • yes, @Liam..Just edited...but it's not the issue. Still continues Commented Aug 15, 2019 at 13:58

1 Answer 1

4

As @R.Richards mentioned, use DomSanitizer:

import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
.
.
.
htmlVar: SafeHtml;

constructor(protected _sanitizer: DomSanitizer) {
  this.htmlVar = this._sanitizer.bypassSecurityTrustHtml(`
    <html>
      <head></head>
      <body>
        <div style="display:flex; color: red;">
          <div>
            <h1>Hello World</h1>
          </div>
        </div>
      </body>
    </html>`);
}

I created a stackblitz to illustrate

Also, as @liam already mentioned you should not have <html>, <header>, or <body> tags as DIV's children

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.