Skip to content

Instantly share code, notes, and snippets.

@armanozak
Created January 28, 2021 10:53
Show Gist options
  • Save armanozak/dc5d8a77f816e9754bef46cdb594125d to your computer and use it in GitHub Desktop.
Save armanozak/dc5d8a77f816e9754bef46cdb594125d to your computer and use it in GitHub Desktop.
[How to Configure Angular Modules Loaded by the Router] A custom child module factory #blog #angular
import {
Compiler,
Injector,
ModuleWithProviders,
NgModuleFactory,
NgModuleRef,
StaticProvider,
Type
} from "@angular/core";
export class ChildModuleFactory<T> extends NgModuleFactory<T> {
get moduleType(): Type<T> {
return this.moduleWithProviders.ngModule;
}
constructor(private moduleWithProviders: ModuleWithProviders<T>) {
super();
}
create(parentInjector: Injector | null): NgModuleRef<T> {
const injector = Injector.create({
parent: parentInjector,
providers: this.moduleWithProviders.providers as StaticProvider[]
});
const compiler = injector.get(Compiler);
const factory = compiler.compileModuleSync(this.moduleType);
return factory.create(injector);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment