Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular

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.

like image 426
Pankaj Kapare Avatar asked Aug 16 '16 20:08

Pankaj Kapare


1 Answers

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

like image 200
Steven Luke Avatar answered Sep 20 '22 20:09

Steven Luke