@ViewChild('question', { read: ViewContainerRef }) container!: ViewContainerRef;
editQuestion(){
const questionFactory = this.componentFactoryResolver.resolveComponentFactory(FermeeSimpleComponent);
const questionComponentRef = this.container.createComponent(questionFactory);
questionComponentRef.instance.add_modalite_simple();
this.container.insert(questionComponentRef.hostView);
}
HTML :
<ol class="questions">
<ng-template #question></ng-template>
</ol>
My code above dynamically creates a component and during this creation calls a function belonging to the created component. This function itself creates a child component. The component is rendered but the child component is not created. I have the following error:
ERROR TypeError: Cannot read properties of undefined (reading 'createComponent')at FermeeSimpleComponent.add_modalite_simple
I want to display the created component and child component
componentFactoryResolveris deprecatedInstead of, you use directly createComponent
BTW. I don't know what are your requirements, but sometimes it's more simple use a simple *ngFor or *ngIf to "create" a component, e.g.
or