angular - 如何找到 Hostlistener 事件目标与 ElementRef 之间的关系?
问题描述
我需要知道,获取点击事件目标是我element
来自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';
}
如何做到这一点?现场演示
有人帮帮我吗?
解决方案
Node.contains()方法应该是您正在寻找的。
if(parent.contains(event)) {
console.log('child')
} else {
console.log('not a child')
}
推荐阅读
- c# - C# 用列表重复列表并合并列表
- php - Api 的参数参数有问题
- python - 列出域列表中的所有子域
- rotation - Healpy:在 hp.ma() 中将遮罩与地图一起旋转与分别产生不同的结果?
- python - app.teardown_appcontext() 在 Flask 中向应用实例注册函数时的调用顺序
- http - HTTP 服务器发送不完整到 http clinet recv
- spring - 在 Kubernetes 中运行的 Spring Batch
- python - VSCode 调试器 FileNotFoundError
- python - subprocess.check_output 追加 \n 即使字符串不包含它
- json - 解析json并放入数组