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-app
injected in yourng-comp3
component and interact with theng-comp1
via theng-app
.I have put an example here
Don't forget that the
ViewChild
andViewChildren
do not get populated untilngAfterViewInit
gets firedThe idea, as may have noticed, is to query for
ng-comp1
in theng-app
component, store that in a public variable and access it from theng-comp3
. The same way I am accessing the propertyname
in my example