AngularMaterial-mat-form-fieldpreventclickeventsusingstopPropagationisnotworking
创始人
2024-10-28 07:01:48
0

我们需要在mat-form-field上添加一个事件监听器,以捕捉click事件并阻止其冒泡。以下是一个示例代码:

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { MatFormField } from '@angular/material/form-field';

@Component({
  selector: 'app-example',
  template: `
    
      
    
  `,
})
export class ExampleComponent implements AfterViewInit {
  @ViewChild('formField') formField: MatFormField;

  constructor(private el: ElementRef) {}

  ngAfterViewInit() {
    this.formField._elementRef.nativeElement.onclick = (event: MouseEvent) => {
      event.stopPropagation();
    };
  }

  handleClick(event: MouseEvent) {
    event.stopPropagation();
    console.log('click event handled');
  }
}

在上面的例子中,我们首先获取mat-form-field的引用,然后在ngAfterViewInit()生命周期钩子中将一个click事件监听器添加到mat-form-field上。在该监听器中,我们调用stopPropagation()以阻止事件冒泡。我们还添加了另一个处理click事件的函数,以确保该事件被正确处理。

相关内容

热门资讯

科普透视!pokemmo免费脚... 科普透视!pokemmo免费脚本,wepoker私人局辅助挂,窍要教程(详细教程)-哔哩哔哩在进入w...
专业透视!智星德州插件!果然是... 专业透视!智星德州插件!果然是真的有辅助app(确实有挂)-哔哩哔哩1、实时智星德州插件透视辅助更新...
详细透视!德普之星有透视辅助吗... 详细透视!德普之星有透视辅助吗,wepoker透视挂底牌,手册教程(有挂技巧)-哔哩哔哩1、每一步都...
详细透视!sohoo辅助!本来... 详细透视!sohoo辅助!本来存在有辅助脚本(有挂助手)-哔哩哔哩1、玩家可以在sohoo辅助线上大...
教你透视!pokerworld... 教你透视!pokerworld破解版下载!真是真的有辅助app(有挂方略)-哔哩哔哩1、上手简单,内...
有挂透视!wejoker内置辅... 有挂透视!wejoker内置辅助,拱趴大菠萝有挂吗,学习教程(真的有挂)-哔哩哔哩1、拱趴大菠萝有挂...
教你透视!aapoker透视脚... 教你透视!aapoker透视脚本下载,aapoker ai插件,烘培教程(有挂分享)-哔哩哔哩1、起...
普及透视!拱趴大菠萝万能辅助器... 普及透视!拱趴大菠萝万能辅助器!竟然真的有辅助工具(有挂猫腻)-哔哩哔哩拱趴大菠萝万能辅助器能透视中...
解谜透视!sohoo竞技联盟辅... 解谜透视!sohoo竞技联盟辅助器!原来是有辅助教程(真的有挂)-哔哩哔哩小薇(辅助器软件下载)致您...
推荐透视!德州局hhpoker... 推荐透视!德州局hhpoker,拱趴大菠萝作必弊方法,手段教程(确实有挂)-哔哩哔哩1、进入游戏-大...