Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何使用模板引用变量访问ng模板中的模板内容_Angular_Angular2 Template_Viewchild_Ng Template - Fatal编程技术网

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树中,那么您将无法访问它。如果它在那里,那么你可以访问它。