New way to do it.
import {ViewContainerRef} from '@angular/core';
/**
your code logic
*/
constructor(private viewContainerRef: ViewContainerRef)
public addTable(): void {
const component = this.viewContainerRef.createComponent(YourElement);
}Below code has been deprecated from angular 12.
import { ApplicationRef, ComponentFactoryResolver, ComponentRef, Injectable, Injector } from '@angular/core';
initializePopUp() {
if (this.compRef && this.compRef.destroy) { this.compRef.destroy(); }
if (this.resolver) {
const compFactory = this.resolver.resolveComponentFactory(PopupComponent);
if (compFactory && this.injector) {
this.compRef = compFactory.create(this.injector);
}
}
if (this.appRef) {
this.appRef.attachView(this.compRef.hostView);
}
}