[英]angular directive interceptor click event
i want Directive click event cancel我想要指令点击事件取消
@Directive({
selector: '[shortConfirm]'
})
export class ShortConfirmDirective implements OnInit{
@HostListener("click", ["$event"])
public onClick(event: any): boolean {
console.info('2222');
event.cancelBubble=true;// ie下
event.stopPropagation();
return true;
}
}
I tried event.我试过事件。 Cancelbubble = true;
取消气泡=真;
event cancel fail事件取消失败
You can try to intercept event during capture phase and cancel it.您可以尝试在捕获阶段拦截事件并取消它。
Try this.尝试这个。
@Directive({
selector: '[shortConfirm]'
})
export class ShortConfirmDirective implements OnInit {
constructor(
private _elementRef: ElementRef,
) { }
ngOnInit() {
fromEvent(this._elementRef.nativeElement, 'click', {
capture: true,
}).subscribe((e: MouseEvent) => {
e.stopImmediatePropagation();
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.