Pankaj Kapare
Pankaj Kapare

Reputation: 7812

Get child/children component(s) using viewContainerRef in Angular2

Within component we can get child components using following constructs:

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

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.

Upvotes: 7

Views: 6798

Answers (1)

Steven Luke
Steven Luke

Reputation: 521

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

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++)

Upvotes: 3

Related Questions