Angular 2 – Services consuming others services before call a method

I think that you could preload such hints at the application startup. For this, you could leverage the APP_INITIALIZER service. The application will wait for the returned promise to be resolved before actually starting.

Here is a sample:

provide(APP_INITIALIZER, {
  useFactory: (service:GlobalService) => () => service.load(),
  deps:[GlobalService, HTTP_PROVIDERS], multi: true
})

The load method would like something like that:

load():Promise<Site> {
  var promise = this.http.get('config.json').map(res => res.json()).toPromise();
  promise.then(config => this.devServer = config.devServer);
  return promise;
}

Then you can directly use the devServer (synchronously)…

See this issue on github for more details:

Leave a Comment