簡體   English   中英

angular 指令攔截器點擊事件

[英]angular directive interceptor click event

我想要指令點擊事件取消

@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;
  }
}

我試過事件。 取消氣泡=真;

事件取消失敗

您可以嘗試在捕獲階段攔截事件並取消它。

嘗試這個。

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM