【发布时间】:2017-10-01 13:12:21
【问题描述】:
假设我们有一个名为 TopComponent 的组件,其模板如下:
<ng-template #top>
<inner-selector #inner></inner-selector>
</ng-template>
代码很简单:
//... imports ...
@Component({
// selector, styles etc
})
export class TopComponent implements AfterViewInit {
@ViewChild('top') topComp : NgbModal;
// NgbModal is a type provided by some 3rd-party library
// I can't modify it
ngAfterViewInit() {
// let's access the #inner component from here
}
}
我可以使用此代码访问#top 组件:
@ViewChild('top') topComponent: TopComponent;
如何从同一个类访问#inner 组件?
我尝试使用@ContentChild('inner'),但仍然得到undefined。
PS。我知道我可以创建另一个组件,使用它而不是 <ng-template> 并从中访问所有必要的子组件。但这可以通过某种方式避免吗?
【问题讨论】:
-
不应该是
@ViewChild('#top')吗? -
@Meir,您的意思是访问
#top组件?没有#符号也可以正常工作。
标签: angular components