'Angular load file content once on startup
I am using Angular 5 & need some configuration data from a .json file before Angular startup. I tried using APP_INITIALIZER as below in app.module.ts
const appConfigLoader = (appConfig: AppConfig) => {
return () => {
return appConfig.loadAppConfig();
};
};
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
useFactory: appConfigLoader,
multi: true,
deps: [AppConfig]
}
]
})
My loadAppConfig function
loadAppConfig() {
const http = this.injector.get(HttpClient);
const configPath = DEBUG_INFO_ENABLED ? 'config/application-dev.json' : 'config/application-prod.json';
return http.get(configPath)
.toPromise()
.then((data) => {
AppConfig.API_URL = data['API_URL'];
});
}
Currently it works well but when I update content of application-dev.json/application-prod.json, Angular also update it to use new content. So I wonder is there any solution if I want it to load only once on startup and doesn't update when I update file content?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
