I want to have a parent view loop through the html of different child components. I'm using Angular8. How can I do this?
I have tried various attempts using ViewChildren, like suggested here and here.
But these and other examples of ViewChildren seem to be going for something different than what I have in mind. Or it could just be my lack of understanding of ViewChildren.
Here is some psuedo-code to express what I have in mind (I know it doesn't work, but hopefully it clarifies the goal here):
*happy-parent-component:
html:
<div *ngFor="let childComponent of childComponents" >
<{{childComponent}} ></{{childComponent}}> <!--I understand this doesn't work-->
</div>
ts:
import { ChildComponent1} from '../child-component1/child-component1.component"
import { ChildComponent2} from '../child-component2/child-component2.component"
...
export class HappyParentComponent implements OnInit {
@ViewChild(ChildComponent1, {static: false})
@ViewChild(ChildComponent2, {static: false})
public childComponents = [ChildComponent1, ChildComponent2] //(I understand this doesn't work...)
*child-component1:
html:
<div><!--fancy html from childcomponent1 --></div>
*child-component2:
html:
<div><!--fancy html from childcomponent2 --></div>
The result on the HappyParentComponent view would be:
<!--fancy html from childcomponent1 -->
<!--fancy html from childcomponent2 -->
Is there a way to do this?
EDIT: Just for more info, the reason I want to add the html of the child components dynamically, instead of just putting childComponen1, childComponent2, etc.. directly into the parent view html, is that which components will be displayed depends on user selections.
So users will make certain selections, and that will change which child components are displayed. And over time, there could end up being a lot of child components.