ng-template的ViewChildren [英] ViewChildren for ng-template

查看:78
本文介绍了ng-template的ViewChildren的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个angular2组件,其模板如下所示:

I have a angular2 component for which the template look like this:

<div>
    <div><ng-template #left></ng-template></div>
    <div><ng-template #right></ng-template></div>
</div>

我希望能够使用ViewChildren检索对所有ng-template元素的引用,但是我不知道我需要在括号之间传递的类型.

I would like to be able to retrieve a reference to all ng-template element using ViewChildren but I have no idea the type I need to pass between bracket.

推荐答案

@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);
}

这篇关于ng-template的ViewChildren的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆