使用angular4在innerhtml中单击动作不起作用 [英] click action in an innerhtml using angular4 not working

查看:312
本文介绍了使用angular4在innerhtml中单击动作不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将锚标记放入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事件那样评估该内容中的任何expressionbinding.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆