Is it possible to reference a module (already compile in umd or es format) and load it dynamically in a already compiled angular application?
I tried to load the module with SystemJsNgModuleLoader.load, but it does seem to work with this kind of use cases.
Thanks
EDIT: Same question (no answer): How to dynamically load external angular 2 module (like served from an external module.bundle.js)
You can do it like this:
@Component({
providers: [
{
provide: NgModuleFactoryLoader,
useClass: SystemJsNgModuleLoader
}
]
})
export class ModuleLoaderComponent {
constructor(private _injector: Injector,
private loader: NgModuleFactoryLoader) {
}
ngAfterViewInit() {
this.loader.load('app/t.module#TModule').then((factory) => {
const module = factory.create(this._injector);
const r = module.componentFactoryResolver;
const cmpFactory = r.resolveComponentFactory(AComponent);
// create a component and attach it to the view
const componentRef = cmpFactory.create(this._injector);
this.container.insert(componentRef.hostView);
})
}
}
Read Here is what you need to know about dynamic components in Angular for more details. Specifically Dynamic module loading and compilation
section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With