Angular 2:单击链接时停止传播父元素的事件 [英] Angular 2 : Stop propagation of parent element's event , when clicking on link
问题描述
我有一个事件冒泡的情况.示例:
I have a case of event bubbling. Example :
<td (click)="doSomething()">
<text [innerHtml]="content">
// content of innerHtml is : <a href="http://google.com"></a>
</text>
</td>
标签是通过innerHtml从另一个组件渲染的.问题:当我点击链接时,元素的点击事件也会被触发.如何解决问题(停止doSomething()的传播),知道事件处理程序(或任何angular 2代码)不能通过innerHtml传递?
The tag is rendered from another component through innerHtml. The problem: when i click on the link, the click event of element is fired also. How to solve the problem (stop the propagation of doSomething()), knowing that event handlers(or any angular 2 code ) can't be passed through innerHtml?
谢谢!
推荐答案
解决方法 可以简单地将 (click)="$event.stopPropagation()"
放在 text
组件,这样事件就不会从托管组件中冒出来.同样的事情可以通过编写指令
来即兴发挥.
Workaround could be simply placing (click)="$event.stopPropagation()"
over text
component, so that event will not get bubbled up from hosting component. Same thing can be improvise by writing a Directive
.
<td (click)="doSomething()">
<text (click)="$event.stopPropagation()" [innerHtml]="content">
// content of innerHtml is : <a href="http://google.com"></a>
</text>
</td>
这篇关于Angular 2:单击链接时停止传播父元素的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!