Use ComponentFactoryResolver to add components dynamically. This is further explained in the Angular Docs. Added a full solution in stackblitz:
ComponentFactoryResolver. In summary this is what I did:
table creation in app.component.ts:
createTable(container) {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(TableComponent);
let viewContainerRef = this.insTable.viewContainerRef;
viewContainerRef.clear();
let componentRef = viewContainerRef.createComponent(componentFactory);
// let e = document.createElement("app-table");
// container.appendChild(e);
}
The directive used as and anchor point table.directive.ts:
import { Directive, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[ins-table]',
})
export class TableDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
And the app.component.html:
<div #charts id="test">
<ng-template ins-table></ng-template>
</div>
And the app.module.ts with proper declarations to make it all work:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ChartModule } from 'angular-highcharts';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { TableComponent } from './table/table.component';
import { TableDirective } from './table/table.directive';
@NgModule({
imports: [BrowserModule, ChartModule, HttpClientModule],
declarations: [AppComponent, TableComponent, TableDirective],
bootstrap: [AppComponent],
providers: [HttpClientModule],
entryComponents: [
TableComponent
]
})
export class AppModule { }
<angular-table></angular-table>inapp component html*ngif="type === 'table' "to<angular-table></angular-table>