简体   繁体   English

Angular4 / Child元素未触发父(单击)事件

[英]Angular4 / Child element not firing parent (click) event

I have a dropdown function which should be called with a click on this div 我有一个下拉功能,只需点击这个div即可调用

<div (click)="toggleDropdown($event)" data-id="userDropdown">
 Username <i class="mdi mdi-chevron-down"></i>
</div>

But the click event is not triggered when I click on the <i> element 但是当我单击<i>元素时,不会触发click事件

toggleDropdown($event) {
    const id = $event.target;
    document.querySelector('[data-drop=' + id.getAttribute('data-id') + ']').classList.toggle('active');
}

Anyway i can make it that the child click event to trigger the parent one? 无论如何,我可以让孩子点击事件触发父母一个?

Plnkr Plnkr

Use $event.currentTarget instead of $event.target 使用$event.currentTarget而不是$event.target

This way you will get the element to which the event handler has been attached. 这样,您将获得已附加事件处理程序的元素。

Plunker Example Plunker示例

Read more 阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM