Loading a dynamic module from another url-angleular4

Is it possible to refer to a module (already compiled in umd or es format) and dynamically load it into an already compiled angular application?

I tried loading the module using SystemJsNgModuleLoader.load, but it seems to work with such use cases.

thank

EDIT: same question (no answer): How to dynamically load an angular 2 external module (e.g. from an external module module.bundle.js)

+4
source share
1 answer

You can do it as follows:

@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's what you need to know about dynamic components in Angular for more details. In particular Dynamic module loading and compilation.

+1
source

Source: https://habr.com/ru/post/1683260/


All Articles