首页 > 解决方案 > Angular 访问父模板元素 ref

问题描述

我有一个父组件,在该 TS 文件中我得到元素 ref 如下:

@ViewChild("custContainer") _custContainer: ElementRef;
const height = this._custContainer.nativeElement.height;

然后我有一个子组件,我需要在其中访问这个父 ElementRef。如何在子组件中执行此操作,并且我相信一旦父组件被渲染,我就需要获取它,因为一旦数据加载到此元素中,我正在使用它来获取高度。

标签: angular

解决方案


如果要访问子组件内部的高度,则可以使用@Input子组件中的属性。

在子组件中

.ts

@Input() height;

并从父组件 html

<child-comp [height]="height"></child-comp>

推荐阅读