1

I'm trying to bind html content to a div in my Angular 6 app but it doesn't work.

Here is the content:

let content = 'Hello <a routerLink="['/Soja']">Nick Soja</a> How are you ?'

Here is the html part:

<div class="content" [innerHTML]="content"></div>

Now only Hello is binded. I don't see the rest of the html content. The expected result is:

'Hello Nick Soja How are you ?'

What's wrong ?

Amadou Beye
  • 2,538
  • 3
  • 16
  • 37

3 Answers3

1

You can't use angular directives inside the innerHTML input value, it expects valid HTML. So that HTML is stripped from the code.

The HTML string being converted to HTML by the innerHTML directive won't be compiled. So that also means you can't use things like databindings inside the HTML string.

enf0rcer
  • 545
  • 2
  • 11
0

Try to remove the routerLink directive. I believe it doesn't work because [innerHTML] cannot compile angular directives and components, only static HTML.

Shahar
  • 2,269
  • 1
  • 27
  • 34
0

just use: tick (``) instead of single quotes ''

content = Hello <a routerLink="['/Soja']">Nick Soja</a> How are you ?;

or try this: define your link in .ts file

link = '/Soja'

and bind it to router link in your template file

<div class="content" > Hello <a routerLink="link">Nick Soja</a> How are you ? </div>

juazsh
  • 11
  • 3