Observables in Angular
Angular uses observables as an interface to handle many common asynchronous operations. Observables provide support for sharing data between the publishers and subscribers in an Angular application.
It is a better technique for handling multiple values than techniques like event handling, asynchronous programming, and promises.
A special feature of Observables is as it can access by a subscriber that subscribes to it. A function is defined to publish values, but the subscribed consumer does not execute it. An observable can provide any number of values at a time.
The API for getting the value is the same in either case, and both setup and logic are handled by the observable. The best part is about subscribing and unsubscribing only for the necessary information.
For example:
You have to define custom events that send observable output data from the child to the parent component.
The HTTP module uses the observables to handle AJAX requests and responses.
Routers and forms modules use observables to listen and respond to user-input events.
Transmitting Data between Components
Angular provides an event emitting class used when publishing values from a component via the @Output() decorator. The event emitter extends RxJSSubject, adding an emit() method to send arbitrary values. When you call to emit(), it passes the emitted value to any subscribed observer’s next() method.
Here is example component that listens for open and close events:
Below is the component definition:
HTTP
Angular’s HttpClient returns the observable from the HTTP method call.
For example, http.get(‘/API) returns an observable. It provides several advantages over Promises-based HTTP APIs:
Observables do not change the server response. You can use a series of operators to change the values as needed.
- HTTP requests are cancellable via the unsubscribe () method.
- Requests can be configured to receive progress event updates.
- Failed requests can be easily retried.
Async Pipe
AsyncPipe subscribes to an observable and returns the latest value emitted by it. The pipe marks the component to be checked for changes when a new value is emitted.
The following example binds the observable time to the component’s view. The observable updates the view with the current time.
Router
The router provides events as observables. We can use the filter() operator from RxJS to look for events of interest and subscribe them to make decisions based on the sequence of events in the navigation process. Below is the example:
Router events
Dynamic routing is an injected router service that uses observables to obtain information about route paths and parameters.
For example, ActivatedRoute.url has an observable that reports a route path or paths.
ActivatedRoute
Reactive forms
Reactive forms use observables to monitor form control values. FormControl properties value change and state change have observables that raise change events.
Subscribing to an observable form-control property is a way to trigger application logic within a component class.
Error Handling:
Observables generate values asynchronously, and thus there is no error in try/catch because it can stop the code regardless of other tasks running at the time.
Instead, we handle errors by specifying an error callback on the observer. An observable can either produce a value (calling the next callback) or call the complete, complete, or error callbacks. When an error occurs, it clears the observable subscription and stops producing that subscription value.
Syntax: