使用Angular2中的viewContainerRef获取子/子组件

Pan*_*are 6 angular

在组件中,我们可以使用以下构造获取子组件:

@ViewChildren(MdVerTabLabelWrapper) _labelWrappers: QueryList<MdVerTabLabelWrapper>;
@ViewChildren(MdVerInkBar) _inkBar: QueryList<MdVerInkBar>;
Run Code Online (Sandbox Code Playgroud)

但是,如果我有viewContainerRef引用,那么如何获取子组件?

基本上我要做的是我有组件列表,我需要以编程方式找到这些组件的子项.

小智 3

ViewContainerRef 类具有用于访问子级的 length 和 get(index) 属性。请参阅此处的 API:

https://angular.io/docs/ts/latest/api/core/index/ViewContainerRef-class.html

因此,要引用特定的子项,您可以使用viewContainerRef.get(childOfInterestIndex),并且要对所有子项进行操作,您可以使用 for 循环,使用类似for(var index = 0; index < viewContenrRef.length; index++)

  • 这些元素实际上是“ViewContainerRef”引用的元素的兄弟元素,而不是子元素。 (2认同)