How to bind html into Angular 2.0 [duplicate]
Asked Answered
B

1

20

In angular 1 binding works like ng-bind-html="htmlValue"

How to bind html in Angular 2.0

Bingle answered 7/1, 2016 at 12:20 Comment(2)
ng-bind-html would sanitize everything to prevent script injection attacks. the solution bellow I think does not do that, but haven't tried it.Fennell
Yes, you're right! I made a test with <em onmouseover="this.textContent='PWN3D!'">click here</em> and the JavaScript code is actually executed on the click...Doorframe
D
49

I think that you could use the innerHtml attribute and bind something on it:

<span [innerHtml]="someHtmlContent"></span>

Here is a sample:

@Component({
  selector: 'first-app',
  template: `
    <span [innerHtml]="value"></span>
  `
})
export class AppComponent {
  constructor(private http:Http) {
    this.value = '<strong>test</strong>';
  }
}
Doorframe answered 7/1, 2016 at 12:23 Comment(3)
thanks @thierry its workingBingle
what if I need to use another component inside [innerHtml], like this, which is not working <span [innerHtml]="<a [routerLink]="/test"></a>"></span> This router link is not getting compiled.Guerrero
@Wishnu Though it's a bit late: in that case you can get away with an *ngIf on the a instead of an [innerHtml].Haustellum

© 2022 - 2024 — McMap. All rights reserved.