停止鼠标事件传播 [英] Stop mouse event propagation
本文介绍了停止鼠标事件传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Angular 2中停止鼠标事件传播的最简单方法是什么?
我应该传递特殊的$event
对象并自己调用stopPropagation()
还是其他方法.例如,在Meteor中,我可以简单地从事件处理程序中返回false
.
What is the easiest way to stop mouse events propagation in Angular 2?
Should I pass special $event
object and call stopPropagation()
myself or there is some other way. For example in Meteor I can simply return false
from event handler.
推荐答案
如果您希望能够将其添加到任何元素而不必一遍又一遍地复制/粘贴相同的代码,则可以创建一条指令来执行这.它很简单,如下所示:
If you want to be able to add this to any elements without having to copy/paste the same code over and over again, you can make a directive to do this. It is as simple as below:
import {Directive, HostListener} from "@angular/core";
@Directive({
selector: "[click-stop-propagation]"
})
export class ClickStopPropagation
{
@HostListener("click", ["$event"])
public onClick(event: any): void
{
event.stopPropagation();
}
}
然后将其添加到所需元素:
Then just add it to the element you want it on:
<div click-stop-propagation>Stop Propagation</div>
这篇关于停止鼠标事件传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文