Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何找到Hostlistener事件目标与ElementRef之间的关系?_Angular_Angular8 - Fatal编程技术网

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')
}