Something

Angular 2: pass an HTML element to a template

The data source that I received passed something like:

<a href="javascript:blahblah...>Something</a> 

And I assigned it to say anchor: HTMLAnchorElement; in the component. When I try to display it in the template as follows:

 {{anchor}} 

It comes out as javascript:0 blehblahek .

What I want to do is just pass the HTML element to the template. How can i do this?

+5
source share
1 answer

You are looking for innerHTML , for example:

Component

 someHtmlCode: string = "<div><b>This is my HTML.</b></div>" 

Template

 <div [innerHTML]="someHtmlCode"></div> 

But if you want to pass a script tag or some other potentially dangerous code, you need to use DomSanitizer .

+11
source

Source: https://habr.com/ru/post/1263309/


All Articles