Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2: pass a HTML Element to template

The data source I get 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 template like this:

{{anchor}}

It comes out as javascript:0 blehblahek.

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

like image 748
lock42 Avatar asked Jan 25 '17 08:01

lock42


1 Answers

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 script tag or some other potentially dangerous code, you need to use DomSanitizer.

like image 151
Stefan Svrkota Avatar answered Sep 20 '22 18:09

Stefan Svrkota