Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click action in an innerhtml using angular4 not working [duplicate]

I am trying to put an anchor tag in an innerHTML and I am using angular 4.

in HTML file: <div [innerHTML]="'myText' | translate">

in Component: const myText = "mytext go here and <a (click)='myFunction()'> click here </a>."

I am not getting the "(click)='myFunction()'" inside in the DOM.

like image 304
KRUSHANU MOHAPATRA Avatar asked Mar 21 '26 21:03

KRUSHANU MOHAPATRA


1 Answers

This will not work, your code is fine but it's by design of Angular.

Whenever you pass some content via innerHTML, Angular is just going to render that content does not evaluate any expression or binding within that content like you did by adding a click event.

So In order to achieve this (as per your requirement), you can wrap your content into another component and pass data via @input property binding and display it into the browser.

For more detail you can read out here -

  • Angular 2 innerHTML (click) binding
like image 151
Pardeep Jain Avatar answered Mar 23 '26 09:03

Pardeep Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!