Angular 如何找到Hostlistener事件目标与ElementRef之间的关系?
我需要知道,获取点击事件目标是我的Angular 如何找到Hostlistener事件目标与ElementRef之间的关系?,angular,angular8,Angular,Angular8,我需要知道,获取点击事件目标是我的元素的子元素,它来自viewChild或在它之外。这有可能找到吗 我没有找到解决办法 代码如下: import { Component, ViewChild, HostListener, ElementRef } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.compone
元素的子元素,它来自viewChild
或在它之外。这有可能找到吗
我没有找到解决办法
代码如下:
import { Component, ViewChild, HostListener, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
@ViewChild('child') element:ElementRef;
@HostListener('document:click', ['$event.target']) onClick(event) {
const parent = this.element.nativeElement;
if(event is childOf(parent)) {
//
} else {
//
}
console.log(parent, event);
}
name = 'Angular';
}
如何做到这一点
有人能帮我吗?请尝试使用这种方法
if (document.getElementById('idOfParentElement').contains(event.target)) {
// do something
}
方法应该是您正在寻找的
if(parent.contains(event)) {
console.log('child')
} else {
console.log('not a child')
}