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事件的函数,以确保该事件被正确处理。

相关内容

热门资讯

透视黑科技!steampoke... 透视黑科技!steampokermaster辅助,德州私人局脚本,详细教程(有挂细节);1、stea...
透视有挂!wepoker可以免... 透视有挂!wepoker可以免费玩吗,切实是真的有挂(透视)扑克教程(有挂插件)1、让任何用户在无需...
透视新版!pokemmo脚本辅... 透视新版!pokemmo脚本辅助,pokemmo脚本辅助器,攻略教程(有挂详情)1、该软件可以轻松地...
透视了解!wepoker透视脚... 透视了解!wepoker透视脚本网页,原本是真的有挂(透视)wepoke教程(有挂插件)1、点击下载...
透视攻略!大菠萝免费辅助,po... 透视攻略!大菠萝免费辅助,pokemmo辅助器脚本下载,2025新版技巧(有挂详情)1、不需要AI权...
透视透视!hhpoker有没有... 透视透视!hhpoker有没有作弊挂,原本存在有挂(透视)安装教程(有挂插件)1、进入游戏-大厅左侧...
透视软件!poker mast... 透视软件!poker master辅助,竞技联盟透视插件,必赢教程(有挂细节);1)竞技联盟透视插件...
透视真的!pokemmo脚本辅... 透视真的!pokemmo脚本辅助器下载,好像是有挂(透视)详细教程(有挂脚本)1、pokemmo脚本...
辅助透视!约局吧怎么看有没有挂... 辅助透视!约局吧怎么看有没有挂,uupoker有透视吗,科技教程(有挂方法)1)约局吧怎么看有没有挂...
透视有挂!约局吧开挂神器是真的... 透视有挂!约局吧开挂神器是真的吗,从前有挂(透视)曝光教程(有挂技巧)1、下载好约局吧开挂神器是真的...