B. Ciervo B. Ciervo - 15 days ago 5
HTTP Question

Angular 2 share/change data got from HTTP service between components

I have to develop an Angular 2 application in which I have an HTTP service whose response is a JSON object like that:

plan = {ID: 'planID', installmentsNumber: 10, rate: 5}


I need these data to be shared between more components and every update to be caught by every component.

Which is the best way?

UPDATE (Nov 23, 2016)

I developed my app using Observable, like you can see in the plunk below:
https://plnkr.co/edit/0k0jr8KrJfFKeNkIV6So?p=info

Solved. Thanks to all.

Answer

You should use RxJs Observable library for that matter. As your questions is broad and you haven't shared a working example I propose to you to read the following blog articles:

How to build Angular 2 apps using Observable Data Services http://blog.angular-university.io/how-to-build-angular2-apps-using-rxjs-observable-data-services-pitfalls-to-avoid/ Angular 2 HTTP Requests with Observables https://scotch.io/tutorials/angular-2-http-requests-with-observables

I prefer BehaviourSubject because you can initialise it easily and set a default state.