假设我们有一个名为
代码很简单:
我可以使用这段代码访问
如何从同一类中访问 #inner 组件?
我尝试使用 @ContentChild('inner'),但仍然得到 undefined。
附注:作为一种解决方法,我可以创建另一个组件,在 中使用它,并从中访问所有必要的子组件。但是否可以以某种方式避免这种情况?
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。
附注:作为一种解决方法,我可以创建另一个组件,在 中使用它,并从中访问所有必要的子组件。但是否可以以某种方式避免这种情况?
@ViewChild('#top')
吗? - undefined#top
组件吗? 没有#
符号也可以正常工作。 - undefined