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

相关内容

热门资讯

现就发布提示!sohoo辅助,... 现就发布提示!sohoo辅助,微乐小程序辅助器,详情透视(有挂猫腻)-哔哩哔哩1、在sohoo辅助插...
透视总结!德州hhpoker是... 透视总结!德州hhpoker是真的吗,hhpoker万能辅助器(透视)真是是有器(哔哩哔哩)1、破解...
目前!德普之星怎么开辅助,微信... 目前!德普之星怎么开辅助,微信小程序游戏破解微乐辅助,详细透视(的确有挂)-哔哩哔哩一、德普之星怎么...
脚本详细!wepoker免费脚... 脚本详细!wepoker免费脚本咨询,we poker插件(透视)其实有器(哔哩哔哩)小薇(辅助器软...
透视脚本!hh poker软件... 透视脚本!hh poker软件,淘宝的微乐辅助是真的吗,详细脚本(有挂秘籍)-哔哩哔哩1、下载好hh...
透视解密!wepoker免费透... 透视解密!wepoker免费透视脚本,wepoker辅助是真的假的(透视)真是存在有脚本(哔哩哔哩)...
出乎意料的是!wepoker可... 出乎意料的是!wepoker可以透视码,微乐如何自己建脚本,解迷透视(有挂解惑)-哔哩哔哩1、这是跨...
透视专业!wpk透视是真的假的... 透视专业!wpk透视是真的假的,wpk辅助软件(透视)一贯有插件(哔哩哔哩)能透视中分为三种模型:模...
透视好友!wejoker辅助软... 透视好友!wejoker辅助软件价格,手机微乐小程序自建房透视免费,解谜透视(有挂存在)-哔哩哔哩1...
透视透视!德普之星辅助器怎么用... 透视透视!德普之星辅助器怎么用,德普软件(透视)都是真的有教程(哔哩哔哩)辅助器是一种具有地方特色的...