Introduction
What is Async Pipe ?
Async Pipe is an impure pipe which automatically subscribes to an observable to emit the latest values. It not only subscribes to an observable but also it subscribes to promise and calls the then method .When the components get destroyed it automatically unsubscribes it to reduce memory leaks.
Advantages of Async Pipe
- Async Pipe makes rendering of data from observable and promise easier.
- For promises it automatically calls then method and
- For observables it automatically calls subscribe and unsubscribe.
Prerequisites
- Basic knowledge of Angular
- Visual Studio Code must be installed
- Angular CLI must be installed
- Node JS must be installed
Step 1
ng new asyncPipe
Step 2
Now, let's create a new component by using the following command,
ng g c async-pipe-example
Step 3
Now, open the async-pipe-example.component.html file and add the following code in the file,
- <div class="card">
- <div class="card-body pb-0">
- <h4 style="text-align: center;">{{SampleMessage}}</h4>
- <div class="row">
- <div class="col-12 col-md-12">
- <div class="card">
- <div class="card-body position-relative">
- <div class="table-responsive cnstr-record product-tbl">
- <table class="table table-bordered heading-hvr">
- <thead>
- <tr>
- <th width="50">Art.No
- </th>
- <th>Brand</th>
- <th>
- Price/Unit</th>
- <th>Provider</th>
- <th>P. Art. N</th>
- <th>S. A/C</th>
- </tr>
- </thead>
- <tbody *ngFor="let product of products$ | async">
- <tr>
- <td align="center">{{product.ArtNo}}</td>
- <td>{{product.Brand}}</td>
- <td>{{product.Price }}</td>
- <td>{{product.Provider}}</td>
- <td>{{product.ProviderArtNo}}</td>
- <td>{{product.SalesAccount}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
Here in the below code we can see how to apply async pipe with our structural direcrtive *ngFor
- <tbody *ngFor="let product of products$ | async">
Step 4
Now, open the async-pipe-example.component.ts file and add the following code in this file,
- import { Component, OnInit } from '@angular/core';
- import { ProductsService } from '../product.service';
- import { Observable } from 'rxjs';
- @Component({
- selector: 'app-async-pipe-example',
- templateUrl: './async-pipe-example.component.html',
- styleUrls: ['./async-pipe-example.component.css']
- })
- export class AsyncPipeExampleComponent implements OnInit {
- products = [];
- products$:Observable<any>;
- SampleMessage="Example of Angular Async Pipe";
- constructor(private _productService:ProductsService) { }
- ngOnInit() {
- //this.getProductsUsingSubscribeMethod();
- this.getProductsUsingAsyncPipe();
- }
- public getProductsUsingSubscribeMethod() {
- this.SampleMessage="Example of Angular Subscribe Method";
- this._productService.getAllProducts().subscribe((data: any) => {
- this.products =data;
- console.log(data);
- })
- }
- public getProductsUsingAsyncPipe() {
- this.SampleMessage="Example of Angular Async Pipe";
- this.products$ =this._productService.getAllProducts();
- }
- }
Here in the below code we are using observables of any type and to get the asynchronous flow data either we can use subscribe method to subscribe the observables or we can simply use 'async pipe' which automatically subscribes to an Observable and returns the latest value and it also unsubscribes automatically to avoid memory leaks.
- public getProductsUsingAsyncPipe() {
- this.SampleMessage="Example of Angular Async Pipe";
- this.products$ =this._productService.getAllProducts();
- }











Join the conversation! Your thoughts help the community grow.