i am trying to load component dynamically using ComponentFactoryResolver
here is my code
import {Component, ComponentRef, ViewContainerRef, ComponentFactoryResolver,
ViewChild, OnInit, OnDestroy, AfterViewInit} from '@angular/core';
import {MyDynamicComponent} from './mycomponent';
@Component({
selector: 'home',
template: '<template #Component1></template>'
})
export class Home implements AfterViewInit {
private componentReference: ComponentRef<any>;
@ViewChild('Component1', { read: ViewContainerRef })
private dynamicTarget: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver) {}
ngAfterViewInit() {
let componentFactory = this.resolver.resolveComponentFactory(MyDynamicComponent);
this.componentReference = this.dynamicTarget.createComponent(componentFactory);
}
this code is working fine
But in my actual implementation the <template #Component1></template>
will be generated dynamically in that case it is not working because the template id is not yet generated when it trying to load the component. is there any way to use the @viewChild after finish generating of the dom. Any help is really appreciated.