[英]Angular 8: Use HostListener to Find DOM Element after a Mouse Click event
How do I find the current DOM element anywhere in the page, after a mouse click?单击鼠标后,如何在页面中的任何位置找到当前的 DOM 元素? Currently trying to use HostListener in Angular 8.
目前正在尝试在 Angular 8 中使用 HostListener。
@HostListener('click') onClick(){
window.alert('Current DOM element is');
}
Set second parameter with $event.target
使用
$event.target
设置第二个参数
@HostListener('click', ['$event.target']) onClick(e){
window.alert('Current DOM element is');
console.log(e);
}
You can also use ElementRef
您还可以使用
ElementRef
import { ElementRef } from '@angular/core';
constructor(private elementRef: ElementRef) {}
@HostListener('click') onClick(){
console.log(this.elementRef.nativeElement);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.