With the DOM snippets below:
<ng-app>
<ng-comp1></ng-comp1>
<ng-comp2>
<ng-comp3></ng-comp3>
</ng-comp2>
<ng-comp1></ng-comp1>
</ng-app>
I would like from ng-comp3 to list all components ng-comp1 in DOM document. There is a way for that ?
ViewChildren list only components inside ng-comp3.
document.querySelector list DOM element, not the component.
The short answer is "No, you cannot directly list them".
What you can do, on the other hand, is to have the
ng-appinjected in yourng-comp3component and interact with theng-comp1via theng-app.I have put an example here
Don't forget that the
ViewChildandViewChildrendo not get populated untilngAfterViewInitgets firedThe idea, as may have noticed, is to query for
ng-comp1in theng-appcomponent, store that in a public variable and access it from theng-comp3. The same way I am accessing the propertynamein my example