0

I am trying to display a string as HTML and have it be able to execute like template code. Ultimately the string comes from the backend. Here is a Stack Blitz that shows my different attempts.

Can this be done in Angular (using 6)?

Scott
  • 485
  • 8
  • 21
  • 1
    Possible duplicate of [Angular HTML binding](https://stackoverflow.com/questions/31548311/angular-html-binding) – Lafexlos May 25 '18 at 14:51
  • Possible duplicate of [Angular2 innerHtml binding breaks data binding](https://stackoverflow.com/q/40279899/1009922). – ConnorsFan May 25 '18 at 15:01
  • You found ways to display the HTML correctly. The problem is that the Angular bindings cannot be processed when HTML code is inserted with `[innerHTML]`, as explained in [this post](https://stackoverflow.com/a/40280167/1009922). – ConnorsFan May 25 '18 at 15:06
  • You may find a few ideas in [this post](https://stackoverflow.com/q/38888008/1009922). – ConnorsFan May 25 '18 at 15:13

2 Answers2

0

Hello try bt using [innerHtml]

Edited

HTML

<div #divref [innerHTML]="html4"></div>

component

  html4 = 'click <a><b>here</b></a>';
  @ViewChild("divref", {read: ElementRef}) divref: ElementRef;


  ngAfterViewInit() {
    // child is set

   this.divref.nativeElement.addEventListener('click', ()=>{
      this.reset();
    })    

  }

demo

Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
0

You need to sanitize your HTML using the DomSanitizer class before displaying the HTML. This is a built-in way to get around Angular's security against XSS.

p4r1
  • 2,490
  • 16
  • 18