angular 5, pass config data to an imported sub-module angular 5, pass config data to an imported sub-module angular angular

angular 5, pass config data to an imported sub-module


I can't find any official way for this scenario, so I find a way but it is a workaround.

You can call the MySubModule.forRoot in the MyModule.forRoot function and assign the providers. (But also you have the import/export the submodule if you have components/directives in the submodule.)

  export const MODULE_OPTIONS = new InjectionToken<ModuleOptions>('MODULE_OPTIONS');    @NgModule({      //You don't have to import/export if MySubmodule doesn't have any component/directive declaration.      imports: [MySubModule],      exports:[MySubModule]  }) export class MyModule {      static forRoot(options: ModuleOptions = {}): ModuleWithProviders {        //Now we are able to provide services/values that uses options in the MySubmodule.        const moduleProviders= MySubmodule.forRoot(options).providers;        moduleProviders.push({provide: MODULE_OPTIONS,useValue: options});        return {ngModule: MyModule,providers: moduleProviders};        }    }