【发布时间】:2019-01-20 13:04:53
【问题描述】:
如何在没有明确声明的情况下访问 Angular 2+ 中 @ViewChild() 的子元素(此处为:<img>)?
在template.html中
<div #parent>
<!-- There can be anything than <img> -->
<img src="http://localhost/123.jpg" alt="">
</div>
在component.ts中
@ViewChild('parent') parent;
public getFirstChild() {
this.firstChild = this.parent.? //
}
目标是能够创建一个通用组件,该组件使用:
<div #parent>
<ng-content></ng-content>
</div>
因此#parent 的子元素需要无需显式声明即可访问。
【问题讨论】:
标签: angular typescript angular-components viewchild