I´m trying to dynamically import components from a module without explicitly importing all components individual. The module consists of two sub modules with each having a component inside.
I have no clue how to create the components from the list of factories which I get from compileModuleAndAllComponentsSync(ExampleModule)
.
Here is my code:
app.component.html
<ng-container *ngComponentOutlet="myComponent;
ngModuleFactory: myModule;">
</ng-container>
app.component.ts
import { Component, Compiler } from '@angular/core';
import { ExampleModule } from '@example/example.module';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private myModule: any;
private myComponent: any;
constructor(private compiler: Compiler) {
const componentList = this.compiler.compileModuleAndAllComponentsSync(ExampleModule);
// const componentList = this.compiler.compileModuleSync(ExampleModule);
console.log(componentList);
this.myModule = null; //<-- What goes here...
this.myComponent = null; //<--...and here?
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ExampleModule } from '@example/example.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ExampleModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
How can I create the components dynamically from my module?