I am developing an Ionic + Angular 5 project.
I need to dynamically load some components. Following the Dynamic Component Loader Example in the Angular documentation I am able to successfully load components and displayed them.
I have a need, however, to be able to loop through the loaded components.
I can easily maintain my own list of components in the parent as I create and destroy the child components but it seems to me that that's ugly and not the "Angular Way".
After much experimentation and research it seems that I cannot use @ViewChildren to access dynamically added components since they have their own views. ViewChildren not finding Dynamically components
Is this correct?
I am able to use viewContainerRef.get() to iterate over the child views and retrieve ViewRefs. But there doesn't seem to be a way to get a reference to the component given a ViewRef?
I notice the 'rootNodes' property of the ViewRef however that property is not documented in the Angular docs View Ref However, it is documented in the EmbeddedViewRef docs Embedded View Ref but that still doesn't seem to give me access to the component.
So the question is when I have a parent that dynamically adds child components how can I from the parent loop through the child components (so I can call one of my methods in the child)? My suspicion is that I am missing something stupid simple, have some fundamental misconception, or that there is a different pattern that I should be using.
I've modified the Dynamic Component Loader example to highlight what I am trying to do.Modified Dynamic Component Loader
let componentRef = viewContainerRef.createComponent(componentFactory); this.newComponent = componentRef;and then inlogViewChildren()doconsole.log(this.newComponent.instance.data);? - pwwpche