如何在父组件内获取相同类型的子组件的引用

时间:2017-02-09 06:50:29

标签: angular

要获取子组件参考,我们有选项 -

@ViewChild(ChildComponent)
childComp: ChildComponent;

我想知道如果我有两次或更多相同的子组件,我怎么能在不同的不同变量中获得所有相同类型的子组件。例如,在我的父模板中,我有 -

<div>
  <child-selector></child-selector>
  <child-selector></child-selector>
</div>

如何在父组件中引用相同类型的子组件?任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:1)

使用ViewChildren

@ViewChildren(ChildComponent)
childComps: QueryList<ChildComponent>;
  

*位于组件模板内部的children元素称为* view children *。另一方面,**元素   在主机的开始和结束标签之间使用   给定组件的元素称为* content children **。

http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/

相关问题