I am trying to assign some value (which I got from http request) from another service to provider token in app.module.ts
.
My service looks like this:
@Injectable()
export class AppConfigService {
private appConfig: AppConfig;
constructor(private http: HttpClient) {}
loadConfigurationData = () => {
this.http.get('assets/config/config.json').subscribe(
(response: AppConfig) => {
this.appConfig = response;
},
error => {
this.appConfig = {
baseHref: '/'
};
}
);
};
getBaseHref(): string {
return this.appConfig.baseHref;
}
}
In my app.module.ts
I am trying to get this value `baseHref`
like that:
@NgModule({
providers: [
...
AppConfigService,
{
provide: APP_INITIALIZER,
useFactory: (appConfigService: AppConfigService) => () => {
appConfigService.loadConfigurationData();
},
deps: [AppConfigService],
multi: true
},
{
provide: APP_BASE_HREF,
useFactory: getBaseHref,
deps: [AppConfigService],
multi: true
},
...
],
})
export function getBaseHref(appConfigService: AppConfigService): string {
return appConfigService.getBaseHref();
}
But I got an error: `Cannot read property 'baseHref' of undefined`
.
Looks like service is not initialized but why? What's the best way to pass this variable from service to provider?