2017-03-02 77 views
1

我有一个angular2组件为其模板是这样的:ViewChildren为NG-模板

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

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

回答

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

嘿,我刚刚写[一对DOM操作的文章(https://medium.com/@maximus.koretskyi/exploring-angular-dom-abstractions- 80b3ebcfc02#.lc9ev4gg0!)使用'ViewContainerRef',看看你是否有时间。 –

+2

关于丢失泛型的视觉工作室代码投诉:'[ts]泛型类型'TemplateRef '需要1个类型参数'我不知道,不应该是'ElementRef'? – bvdb

+0

我无法看到泛型参数将实现什么作用。 'TemplateRef '应该做恕我直言。 –