Angular 查看ng模板的子对象
我有一个angular2组件,其模板如下所示:Angular 查看ng模板的子对象,angular,Angular,我有一个angular2组件,其模板如下所示: <div> <div><ng-template #left></ng-template></div> <div><ng-template #right></ng-template></div> </div> 我希望能够使用ViewChildren检索对所有ng template元素的引用,但我不知道需要在括号
<div>
<div><ng-template #left></ng-template></div>
<div><ng-template #right></ng-template></div>
</div>
我希望能够使用
ViewChildren
检索对所有ng template
元素的引用,但我不知道需要在括号之间传递的类型。嘿,我刚刚使用ViewContainerRef
编写了一篇文章,如果您有时间,请看一看。visual studio代码抱怨缺少泛型:[ts]泛型类型“TemplateRef”需要1个类型参数
我不知道,它是否应该是ElementRef
?我看不出泛型参数将扮演什么角色TemplateRef
应该执行IMHO。泛型类型用于模板上下文对象。如果不使用上下文对象,只需使用any
@GünterZöchbauer当模板从ngTemplateOutlet打印出来时,是否可以查询该模板?
@ViewChildren(TemplateRef) templates: QueryList<TemplateRef>;
ngAfterViewInit() {
console.log(this.templates.toArray());
}
@ViewChild('left') left:TemplateRef;
@ViewChild('right') right:TemplateRef;
ngAfterViewInit() {
console.log(this.left, this.right);
}