Using Observables From Other Sources
In the example above we created Observables
from scratch which is especially useful in understanding the anatomy of an Observable
.
However, we will often create Observables
from callbacks, promises, events, collections or using many of the operators available on the API.
Observable HTTP Events
A common operation in any web application is getting or posting data to a server. Angular applications do this with the Http
library, which previously used Promises
to operate in an asynchronous manner. The updated Http
library now incorporates Observables
for triggering events and getting new data. Let's take a quick look at this:
This basic example outlines how the Http
library's common routines like get
, post
, put
and delete
all return Observables
that allow us to asynchronously process any resulting data.
Observable Form Events
Let's take a look at how Observables
are used in Angular forms. Each field in a form is treated as an Observable
that we can subscribe to and listen for any changes made to the value of the input field.
Here we have created a new form by initializing a new FormControl
field and grouped it into a FormGroup
tied to the coolForm
HTML form. The Control
field has a property .valueChanges
that returns an Observable
that we can subscribe to. Now whenever a user types something into the field we'll get it immediately.
Last updated