If you have done some work with Angular2, you know, you organize your page in components. For example, you might have a component with a login form and another showing the login status - like a top menu or something like that. When you have that, you need a way to allow communication between these components so the login form can let the other ones know the login status has changed. That’s where the Observable classes come in handy.
What is an Observable?
It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O. Angular2 uses this library, so it is encouraged to follow this pattern in your applications.
Problem to solve
To demonstrate how you can implement Observables in your application, I will create a prototype of a problem I mentioned above. It will be a simple angular page with three boxes - components. One will have two buttons - login and logout - second will be showing the login status and the third will change its background color with the status change. You can see full demo code here: https://plnkr.co/edit/0c9xQRHBgMrtsJXCZC3T?p=preview
Main app component
I have created an entry point of the application:
As you can see, it just adds three components as mentioned above.
This is a very simple component with two buttons which are being shown based on loginStatus variable:
This one just shows if the user is logged in or not.
I won’t be pasting login-background source code as it’s nearly the same as this one and just serves a purpose of showing you there can be many components using the observable, which I will create in a moment.
Adding service with an Observable
With the current code we got an application which allows user to log in and two other components which should show the login status. The only problem is they don’t know this status. To solve this problem, I have created a LoginService class:
As you can see, the code is quite simple. There are two fields - status and observer. Observer is a private property holding… an Observer :) Status is a public one, which allows other components to subscribe to the observer. There is also a changeState function, which components can call to let others know something has changed. Few things to notice here:
- share() - creates a subscription, which can be consumed by others
- this.observer.next(newState) - sends a new value to all objects subscribed to the observer
Using LoginService in LoginForm
First of all we need to add LoginService to providers section of bootstrap call in main.ts:
This is needed by Angular to wire up the dependency injection properly. If you’d forget to do it, you’d get an ‘No provider for LoginService!’ exception.
Next, we need to modify LoginForm class:
I have added LoginService as a dependency in the controller - this way it will be injected into our class and allow us to use it. It’s important, because this way we will get the same instance of the service in all components, so they can communicate with each other.
I have also added calls to changeState function in login() and logout() methods.
Subscribing to an Observer
The only part left is to subscribe to LoginService Observer: