为什么Angular 2中的自定义组件单击功能会触发两次 [英] Why click function triggers twice for custom component in Angular 2

查看:85
本文介绍了为什么Angular 2中的自定义组件单击功能会触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义组件click函数被触发两次-自定义组件的事件和样品级事件均被触发.

My custom component click function is triggered twice - both custom component's event and sample level event are triggered.

这是我的笨蛋:

https://plnkr.co/edit/wp2iWh7OStdPm5uXsWbP?p=preview

推荐答案

因为您已在子组件和父组件上将其绑定两次.默认情况下,mouseEvent从子组件传播到父组件.您可以停止将事件传播到父组件.

Because you have bound it twice on the child component and on the parent component. The mouseEvent propagates from the child component to the parent component by default. You can stop propagation of event to parent component.

<div (click)="divClick($event)">Custom Div Clcik here!</div>

班级:

divClick(event) {
    event.stopPropagation();
    alert("divClick");
}

这篇关于为什么Angular 2中的自定义组件单击功能会触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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