0

I have a component in my Angular2 app that loads some particular data (i.e. an ID) that is absolutely necessary for all other components who use that data to load other stuff from the backend.

I've run into a problem when I refresh the browser that certain components start making their HTTP requests before the important one with the ID has returned what poses a problem as they need that ID.

Does anyone have a proper solution for that problem in Angular 2?

Stefan
  • 1,590
  • 3
  • 18
  • 33
  • Please provide the code that allows to reproduce the problem, demonstrates what you try to accomplish, what you tried and where you failed. Your question only allows general answers that may or may not fit your problem - "like use Observables and properly chain them using the right operators" – Günter Zöchbauer Nov 22 '16 at 19:28
  • I can't really provide the code, the app is huge. But the problem can be reduced, susch that I have two components in my app, both use a service that talks to a backend. In order to be able to perform the backend call for component 2, I need the result from the backend call from service 1. The two services and components do not know each other directly, though. – Stefan Nov 22 '16 at 19:48
  • 1
    Then you can make the 2nd service call wait for the 1st service call within the same service. I think something like http://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-call-in should work for your case. The 2nd service call and the components can invoke the method that makes the 1st service call. Both will get the same result. When the 2nd service call receives the response it can make it's own call. You can make the 2nd service call wait for the 1st one using `mergeMap` (alias `flatMap`) – Günter Zöchbauer Nov 22 '16 at 19:53
  • for the 2nd component to receive the result when both calls are completed. – Günter Zöchbauer Nov 22 '16 at 19:53
  • That helps, thanks a lot – Stefan Nov 22 '16 at 20:04

0 Answers0