使用angular4在innerhtml中单击动作不起作用 [英] click action in an innerhtml using angular4 not working
问题描述
我正在尝试将锚标记放入innerHTML中,并且使用的是角度4.
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>."
我没有在DOM中获得(click)='myFunction()'".
I am not getting the "(click)='myFunction()'" inside in the DOM.
推荐答案
这将不起作用,您的代码很好,但是它是由Angular设计的.
This will not work, your code is fine but it's by design of Angular.
无论何时通过innerHTML
传递某些内容,Angular都将渲染该内容,而不像添加click事件那样评估该内容中的任何expression
或binding
.
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.
因此,为了实现此目标(根据您的要求),您可以将内容包装到另一个组件中,并通过@input
属性绑定传递数据并将其显示在浏览器中.
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 -
这篇关于使用angular4在innerhtml中单击动作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!