angular - Angular 访问父模板元素 ref
问题描述
我有一个父组件,在该 TS 文件中我得到元素 ref 如下:
@ViewChild("custContainer") _custContainer: ElementRef;
const height = this._custContainer.nativeElement.height;
然后我有一个子组件,我需要在其中访问这个父 ElementRef。如何在子组件中执行此操作,并且我相信一旦父组件被渲染,我就需要获取它,因为一旦数据加载到此元素中,我正在使用它来获取高度。
解决方案
如果要访问子组件内部的高度,则可以使用@Input
子组件中的属性。
在子组件中
.ts
@Input() height;
并从父组件 html
<child-comp [height]="height"></child-comp>
推荐阅读
- swift - 在 Swift 中,通过用透明颜色覆盖颜色来计算没有透明度的颜色
- scala - 如何验证我的 spark-dataframe 是否实际被填充?
- python - Python如何拥有一个我可以执行和导入的模块
- django - Django:尝试使用自引用默认值进行用户迁移
- html - :checked 当复选框输入不在需要设置样式的项目旁边时不起作用
- algorithm - 生成和解决一个没有边界的迷宫?
- html - 如何使用 HTML IFRAME 元素嵌入内容?
- powershell - 在同一命令中搜索隐藏和可见目录
- c# - 'Microsoft.AspNetCore.Identity.UserManager' 类型没有服务:尝试扩展 IdentityUser?
- android - 使用 firebase 实现异构 RecyclerView