I am trying to provide the APP_BASE_HREF injection token in a Lazy-Loading Module,
however, the factory method is not called at all.
Here https://github.com/MaurizioCasciano/Angular-Lazy-Injection/branches you can find the corresponding minimal project, with two separated branches:
- app-provider-root (working custom APP_BASE_HREF)
- lazy-provider-root (not working custom APP_BASE_HREF)
You can also try the two branches on StackBlitz:
- https://stackblitz.com/github/MaurizioCasciano/Angular-Lazy-Injection/tree/app-provider-root
- https://stackblitz.com/github/MaurizioCasciano/Angular-Lazy-Injection/tree/lazy-provider-root
If I move the same code snippet to the static AppModule it gets called as expected.
providers: [
{
provide: APP_BASE_HREF,
useFactory: appBaseHrefFactory
}
]
export function appBaseHrefFactory() {
console.log("APP MODULE window.location.pathname: " + window.location.pathname);
return window.location.pathname;
}
How can I force the APP_BASE_HREF to be provided also from the Lazy-Loading module?
Update
If I inject the value in the module constructor, the factory method gets called, but the value is not picked up by the imported module/service.
export class RemoteModule {
constructor(@Inject(APP_BASE_HREF) private apipBaseHref: string) {
console.log("Remote APP BASE HREF: " + appBaseHref);
}
}