lock42
lock42

Reputation: 845

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?

Upvotes: 7

Views: 6436

Answers (1)

Stefan Svrkota
Stefan Svrkota

Reputation: 50643

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.

Upvotes: 13

Related Questions