Angular的currentTarget访问属性
创始人
2024-10-24 02:31:40
0

在Angular中,可以使用@ViewChild装饰器来获取当前元素的引用,并通过.nativeElement属性来访问该元素的属性。

以下是一个示例代码:

import { Component, ElementRef, ViewChild } from '@angular/core';

@Component({
  selector: 'app-example',
  template: `
    
  `
})
export class ExampleComponent {
  @ViewChild('myButton', { static: true }) myButton: ElementRef;

  handleClick(event: MouseEvent) {
    const target = event.currentTarget as HTMLButtonElement;
    console.log(target.textContent); // 输出:Click me

    // 使用ViewChild获取的元素引用
    const nativeElement = this.myButton.nativeElement as HTMLButtonElement;
    console.log(nativeElement.textContent); // 输出:Click me
  }
}

在上述代码中,我们使用@ViewChild装饰器来获取模板中的myButton元素的引用,并在handleClick方法中访问了currentTarget属性的值。另外,我们也通过this.myButton.nativeElement来访问myButton元素的属性。注意要进行类型断言,将其转换为HTMLButtonElement类型。

相关内容

热门资讯

透视新版"wepok... 透视新版"wepoker有没有插件"详细辅助透牌教程(往昔是有挂);最新版2024是一款经典耐玩的益...
透视线上"wepok... 透视线上"wepokerplus万能挂"详细辅助扑克教程(果然真的是有挂);1分钟了解详细教程(微信...
透视有挂"佛手大菠萝... 1、透视有挂"佛手大菠萝辅助"详细辅助2025新版技巧(从前有挂);详细教程。2、佛手大菠萝辅助透视...
透视科技"xpoke... 透视科技"xpoker透视辅助"详细辅助揭秘教程(本来是真的有挂);1.xpoker透视辅助 ai辅...
透视苹果版"德州局h... 透视苹果版"德州局hhpoker"详细辅助透明挂教程(起初是有挂);建议优先通过德州局hhpoker...
透视软件"hhpok... 透视软件"hhpoker真的假的"详细辅助2025版教程(原来真的是有挂);亲真的是有正版授权,小编...
透视透视"hhpok... 您好,hhpoker透视脚本下载这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
透视讲解"hhpok... 透视讲解"hhpoker是真的还是假的"详细辅助实用技巧(总是有挂);1.hhpoker是真的还是假...
透视真的"wepok... 透视真的"wepoker辅助分析器"详细辅助扑克教程(原先是真的有挂);wepoker辅助分析器软件...
透视挂"aa pok... 透视挂"aa poker辅助"详细辅助2025新版总结(果然真的有挂)是一款可以让一直输的玩家,快速...