angular - 获取 NgTemplate 中包含的所有子组件的宽度
问题描述
我在模板中有这个标记。我可以使用它遍历它的组件,get
但它没有返回一个允许我深入挖掘 HTML 属性的对象。
<ng-template #container></ng-template>
我在container
上面动态添加组件。
我的组件代码
@ViewChild("container", { read: ViewContainerRef }) appAsideContainer: ViewContainerRef;
for (var x = 0; x < this.appAsideContainer.length; x++) {
let c: ViewRef = this.appAsideContainer.get(x);
console.log(c);
}
c
不给我访问权限,element
或者nativeElement
我可以得到offsetWidth
有没有其他方法可以实现这一目标?
解决方案
它可以通过rootNodes
在转换为之后使用属性来实现EmbeddedViewRef
:
for (var x = 0; x < this.appAsideContainer.length; x++) {
const viewRef = this.appAsideContainer.get(x) as EmbeddedViewRef<any>;
const rootNode = viewRef.rootNodes[0];
console.log(rootNode);
}
推荐阅读
- html - 限制用户使用 Angular 输入特殊字符
- python - 如何从 redirect_uri 获取访问令牌?
- python - 如何处理包含的数据 \n 而无需在 Python 中为 CSV 输入新单元格?
- authentication - 如何在服务器上对身份验证服务进行本地调用?
- python - Python 是否评估前向引用的类型提示?
- mysql - mysql,使用默认值创建列
- python - 如何循环转换DF中的值?
- firebase - get() 和 getAfter() 之间的 Firebase 安全规则差异
- python - tkinter 画布未绘制顶部和左侧线
- javascript - 如何将弹出式浏览器窗口置于最前面,使其位于所有浏览器窗口之上?