Summary:
What is the benefit of using a service's local variable instead of subscribing to an observable within the service?
Illustrative Example:
In the provided plunker, there are two components and a service. Both components share an Observable stored in the service.
In the service, I update a public variable and emit that value to the subscribers.
This piece of code appears redundant to me, but it is commonly used in Angular 2 tutorials.
src/number.ts
this.num = {
num: new Date().getTime()
};
this.observer.next(this.num);
What are the advantages of each approach? Personally, I lean towards the subscription method, but they appear to achieve the same result. What am I overlooking?
NOTE: The setInterval and NgZone code is only for demonstration purposes. In a real application, this data would be fetched through HTTP requests, updating the variable and sending the data to the subscribers. Since we are just fetching time every second, NgZone had to be utilized as well.