Observables and RxJS in Angular
What are Observables?
Observables are a way to handle asynchronous data and events in Angular. Think of them as data streams that can emit multiple values over time.
RxJS: Reactive Programming
RxJS (Reactive Extensions for JavaScript) is a library that provides tools for working with Observables. It allows you to create, compose, and consume data streams declaratively.
Creating an Observable
You can create an Observable using the Observable.create()
function or the new Observable()
constructor.
Example:
Subscribing to an Observable
To receive the values emitted by an Observable, you must subscribe to it using the subscribe()
method.
Example:
RxJS Operators
RxJS provides a wide variety of operators to transform, filter, and combine Observables. Some examples are map
, filter
, reduce
, merge
, etc.
Example with the map
operator: