Angular 如何使用模板引用变量访问ng模板中的模板内容
我想知道是否可以使用模板引用变量访问Angular 如何使用模板引用变量访问ng模板中的模板内容,angular,angular2-template,viewchild,ng-template,Angular,Angular2 Template,Viewchild,Ng Template,我想知道是否可以使用模板引用变量访问ng template中的模板组件? 我尝试过使用@ViewChild和@ContentChild,但它们似乎都不起作用 示例:我想访问应用程序common的内容 <ng-template #first> <app-common #second><app-common> </ng-template> @ViewChild('second') second1: ElementRef; @ContentChild(
ng template
中的模板组件?
我尝试过使用@ViewChild和@ContentChild,但它们似乎都不起作用
示例:我想访问应用程序common的内容
<ng-template #first>
<app-common #second><app-common>
</ng-template>
@ViewChild('second') second1: ElementRef;
@ContentChild('second') second2: ElementRef;
ngAfterViewInit() {
console.log(this.second1);
}
ngAfterContentInit() {
console.log(this.second2);
}
@ViewChild(“第二个”)second1:ElementRef;
@ContentChild('second')second2:ElementRef;
ngAfterViewInit(){
console.log(this.second1);
}
ngAfterContentInit(){
console.log(this.second2);
}
是用于呈现HTML的角度元素。它是
从不直接显示。事实上,在渲染视图之前,角度
将
及其内容替换为注释
只要您在某个地方使用模板,例如:
<ng-content *ngTemplateOutlet="first"></ng-content>
您还可以通过Viewchild访问它的内容。您可以显示您使用“@ContentChild”尝试过的内容吗?它应该与“@ContentChild”一起使用。也许你做了什么wrong@PierBJX我刚刚添加了ContentChild,并对ViewChild进行了TriedId检查DOM树组件是否已实际渲染?如果它不在DOM树中,那么您将无法访问它。如果它在那里,那么你可以访问它。