我有一个 angular2 组件,其模板如下所示:
<div>
<div><ng-template #left></ng-template></div>
<div><ng-template #right></ng-template></div>
</div>
我希望能够使用 ViewChildren
检索对所有 ng-template
元素的引用,但我不知道我需要在括号之间传递的类型。
最佳答案
@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);
}
关于angular - ng-template 的 ViewChildren,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42567236/