angular - ng-template 的 ViewChildren

标签 angular

我有一个 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/

相关文章:

javascript - NavigationEnd 的 url 属性返回未定义(Angular 6)

javascript - Ionic 2 - 主 ionic WebView 中是否可以有另一个 WebView ?

javascript - 将插值数据绑定(bind)到数组的现有元素

javascript - Angular 2( ionic 2): intercept ajax requests

Angular 8 如何从 ngOnInit 中的可观察值中获取值(value)以及它们的行为方式

javascript - 如何将 zingchart 实现到 angular2

angular - 测试依赖于服务的管道

angular - 调度多个 Action 并等待完成发送下一个

angular - Ionic 4 ion-content 滚动到底部

html - 我可以在 ngStyle 中使用 lighten css 属性吗?