使用Angular2中的viewContainerRef获取子组件 [英] Get child/children component(s) using viewContainerRef in Angular2

查看:32
本文介绍了使用Angular2中的viewContainerRef获取子组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Within component we can get child components using following constructs:

@ViewChildren(MdVerTabLabelWrapper) _labelWrappers: QueryList<MdVerTabLabelWrapper>;
@ViewChildren(MdVerInkBar) _inkBar: QueryList<MdVerInkBar>;

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

However if I have viewContainerRef reference then how do I get child component(s)?

基本上,我想做的是获得组件列表,并且需要以编程方式找到这些组件的子代.

Basically what I am trying to do is I have list of components and I need to find children of those components programmatically.

推荐答案

ViewContainerRef类具有用于访问子级的length和get(index)属性.在此处查看API:

The ViewContainerRef class has length and get(index) properties for accessing children. See the API here:

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

因此,要引用特定的子代,请使用viewContainerRef.get(childOfInterestIndex),并使用诸如for(var index = 0; index < viewContenrRef.length; index++)

So to reference a specific child, you use viewContainerRef.get(childOfInterestIndex), and to work on all children you use a for loop, using something like for(var index = 0; index < viewContenrRef.length; index++)

这篇关于使用Angular2中的viewContainerRef获取子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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