Angular 未定义祖先的ViewChild的ElementRef

Angular 未定义祖先的ViewChild的ElementRef,angular,Angular,我正在尝试获取单击元素的远祖的维度(以确定弹出窗口应该放在哪里),但遇到了一些问题。我定义的ElementRef是未定义的 祖先元素: <div class="tab-content" #tabContent *ngIf="registry$ | async as registry"> //subcomponent </div> 我听说ViewChild和*ngIf有问题,但是这个组件是一个祖先,所以上面的所有内容都必须已经创

我正在尝试获取单击元素的远祖的维度(以确定弹出窗口应该放在哪里),但遇到了一些问题。我定义的ElementRef是未定义的

祖先元素:

<div class="tab-content" #tabContent *ngIf="registry$ | async as registry">
  //subcomponent
</div>

我听说ViewChild和*ngIf有问题,但是这个组件是一个祖先,所以上面的所有内容都必须已经创建好了,不是吗?

当tabContent可用时,您可以使用setter来侦听,因为它依赖于可观察的内容

tabContent: ElementRef;

@ViewChild("tabContent", { static: false, read: ElementRef }) set _tabContentElement(tabContent: ElementRef) {

    if (tabContent) {
      this.tabContent = tabContent;
    }
  }

*首先,该元素的可见性取决于可观察对象,因此角度的
ngOninit
(ViewChild static=true)或
ngAfterViewInit
(ViewChild static=false)可能在可观察对象发射之前已通过。您还可以查看
ViewChild
的第二个参数来了解我的意思。仍然未定义。@jugglervr-确保从可观察对象返回的值是“truthy”值抱歉,我不清楚。哪个observable返回的值?注册表$observable w/c async pipe订阅tooh,是的,它是一个对象(计算结果为truthy,否则*ngIf不会启动。它在祖先中启动,所以它必须返回一个值,否则我所在的组件甚至不会生成)
tabContent: ElementRef;

@ViewChild("tabContent", { static: false, read: ElementRef }) set _tabContentElement(tabContent: ElementRef) {

    if (tabContent) {
      this.tabContent = tabContent;
    }
  }