Using an Observable with the async pipe to synchronously bind data to your Angular templates
As you learned in the previous recipe, it is crucial to unsubscribe the streams you subscribe to. What if we had an even simpler way to unsubscribe them when the component gets destroyed—that is, letting Angular take care of it somehow? In this recipe, you'll learn how to use Angular's async
pipe with an Observable to directly bind the data in the stream to the Angular template instead of having to subscribe in the *.component.ts
file.
Getting ready
The project for this recipe resides in chapter05/start_here/using-async-pipe
.
- Open the project in VS Code.
- Open the Terminal and run
npm install
to install the dependencies of the project. - Once done, run
ng serve -o
.This should open the app in a new browser tab. As soon as the page is opened, you should see something like this: