liveBook · Manning

Observable

As explained in Pro Angular 16

An observable is a reactive abstraction representing a sequence of values that can be emitted asynchronously over time.

An observable represents a sequence of values produced over time. In Angular, RxJS provides the Observable<T> class; T identifies the type of each value. An Observable<string>, for example, produces string values. Unlike a Promise, which represents a single result, an observable can provide ongoing notifications. section 10.5.1

Call subscribe to respond when values arrive instead of repeatedly checking for them. A subscription can handle each new value, an error, and the end of the sequence. If only values matter, pass subscribe a single function. section 10.5.1

Table 10.5. The Observable<T> subscribe argument properties
Name
Description
next
This function is invoked when a new value is produced.
error
This function is invoked when an error occurs.
complete
This function is invoked when the sequence of values ends.

Use the subscribe handler table to choose between next, error, and complete callbacks. section 10.5.1

A value every 500 milliseconds

A Ticker exposes value: Observable<number> = interval(500). A component subscribes and assigns each received number to tickerValue, which its template displays. section 10.5.1

Observables also connect to several Angular APIs:

  • HttpClient returns observable results for HTTP requests. An order repository can preserve Observable<Order> when it delegates saving to a REST data source. section 23.8 section 6.6.1
  • A FormControl’s valueChanges emits control values. Set updateOn to blur when changes should be emitted after the input loses focus rather than on the default change event. section 21.3.1
  • A QueryList’s changes observable notifies a directive when matching content children change. Subscribe after content initialization, when the query property is available. section 15.4.2

When the rest of an application expects a signal, toSignal converts an observable into one. For example, a product repository converts its REST data source’s products observable into a Signal<Product[]> with an initial empty array. section 6.6.1

How Pro Angular 16 covers it

How deep it goes
Defines Observable<T> and subscribe callbacks through a ticker, then applies subscriptions to framework APIs without explaining RxJS internals.
Examples
Uses an interval ticker, REST-backed order saving, form input changes, and content-query notifications.
What it assumes
Introduces Observable<T> and subscribe from their basics; treats familiarity with JavaScript Promises as optional.
What you can do after
Apply it via RxJS and Angular APIs to subscribe to emitted values and convert an observable to a signal when needed.
How it connects
Connects observables to RxJS, Promises, HttpClient, signals, FormControl, QueryList, and change detection.

Also explained in

Compare how 5 books explain observable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.