Reputation: 384
I need to get configuration from server before the application bootstraps. I was able to do that using this in providers in my main ngModule:
{
provide: APP_INITIALIZER,
useFactory: (links: Links) => () => links.init(),
deps: [Links, Http],
multi: true
}
Links service:
init(): Promise<any> {
var observable = this.http.get('someUrl').map(res => res.json());
observable.subscribe(res => {
this.config = res;
});
return observable.toPromise();
}
This code gets executed before the application bootstraps, but the response from server is not present until after my app asks for Links.config. How do I force the app to not bootstrap until after the Promise gets resolved? I tried promise.resolve(), but it didn't help.
The way I used providers I thought I forced the app to use the same instance of Links and also I thought the data would be already present there. What am I doing wrong please?
Upvotes: 3
Views: 5352
Reputation: 917
As suggested at #37611549 and #35215112, the @ngx-config/core utility can be helpful to get your config from server during the app initialization.
You can achieve similar results by fetching data within SystemJS imports. However, I don't suggest such an approach - because it will create a risk that your config values won't be statically analyzable during AoT compilation.
Upvotes: 2
Reputation: 317
Please look at the work around that I have used here
Loading server side settings angular2 2.0.1
In short guard your route with a CanActivate class using a Promise which loads the config settings should also work.
Ensures that your settings are available when the corresponding components are constructed. (using the APP_INITIALIZER did not restrict the constructor being called, so I had to use this technic, Also please make sure, you dont export all the components in the exports:[] in the module)
Upvotes: 0
Reputation: 296
In my code I am doing exactly same and it's working only difference is that I am converting the request to Promise before mapping.
public load(): Promise<void> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let url = this.urls.settings;
return this.http.get(url, { headers }).toPromise().
then(configs => {
this.startupSettings = configs.json();
}).catch(err => {
log(err);
});
}
...............................
{
provide: APP_INITIALIZER,
useFactory: (config: StartupConfigurationService) => () => config.load(),
deps: [StartupConfigurationService],
multi: true
}
I don't sure it will make sense but try may be it will solve
Upvotes: 3