Pipes In Angular 2

Introduction

AngularJS 1.x has filters which are used for many common uses, like formatting dates, string display in upper or lower case etc. These filters are known as "Pipes" in Angular 2.

Pipes allow us to change the data inside the template. Normally, a pipe takes the data and transforms this input to the desired output. There are many built-in pipes in Angular 2. In this article, we will cover build-in pipes of Angular 2 applications.

To demonstrate the example, I have already created an Angular 2 app. Within component, I have created a member variable which is used in template HTML.

App.component.ts

  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/PipesExample.html'  
  5. })  
  6. export class AppComponent {   
  7.     joiningDate = new Date(1990, 4, 15, 15, 12, 10);  
  8.     name = 'Jignesh';  
  9.   grade = 0.99;  
  10.     rating = 8.36;  
  11.     priceEuro = 80.6;  
  12.     priceUsd = 145.6;  
  13.     description = "This is my Test";  
  14.               testArray:Array<any> =[{id:1, name:'Test 1'},{id:2, name: 'Test 2'}];  
  15. }  
PipesExample.html

<h4>Pipes in Angular 2 Application</h4>

Date Pipes

Date Pipe is very similar to date filter in AngularJS 1.x. Following are some examples of date pipes. Here, we have various options to format the date using these pipes.

PipesExample.html
  1. <div>  
  2.     <h4>Date Pipe Example</h4>  
  3.     <b>Date format:</b> My joining Date is {{joiningDate | date}}.<br/>  
  4.     <b>Specific Date Format:</b> My joining Date is {{joiningDate | date:"dd/MM/yyyy"}}<br/>  
  5.     <b>Full Date Format:</b> My joining Date is {{joiningDate | date:"fullDate"}}<br/>  
  6.     <b>Short Time format:</b> My joining Date is {{joiningDate | date:"shortTime"}}<br/>  
  7.     <b>Medium Date format:</b> My joining Date is {{joiningDate | date:"mediumDate"}}<br/>  
  8. </div>  
Output



Uppercase Pipe

Uppercase Pipe is similar to uppercase filter in AngularJS 1.x. It converts our input text into uppercase letters.

PipesExample.html
  1. <div>  
  2.     <h4>Upper Case Pipe Example</h4>  
  3.     My name is {{name | uppercase}}!  
  4. </div>  
Output



Lowercase Pipe

Lowercase Pipe is similar to lowercase filter in AngularJS 1.x. It converts our input text into lowercase.

PipesExample.html
  1. <div>  
  2.     <h4>Lower Case Pipe Example</h4>  
  3.     My name is {{name | lowercase}}!  
  4. </div>  
Output



Percentages/Number Pipe

The percent and decimal are new pipes in Angular 2. These pipes take arguments which provide information about decimal points in a digit i.e. how many integers and fraction numbers to be display in a formatted output. We need to pass the argument in format {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.

PipesExample.html
  1. <div>  
  2.     <h4>Decimals/Percentages/Number Pipe Example</h4>  
  3.     Grade : {{grade | percent:'.3'}} <br/>  
  4.     Rating : {{rating | number:'3.1-2'}}  
  5. </div> 
Output



Currency Pipe

Currency pipe is similar to currency filter in AngularJS 1.x. The Angular 2 Currency pipe also accepts parameters. We can pass a currency symbol as a parameter in the Currency pipe.

PipesExample.html
  1. <div>  
  2.     <h4>Currency Pipe Example</h4>  
  3.     <b>Euro: </b> My Laptop price is {{priceEuro | currency:'EUR':true}} <br/>  
  4.     <b>USD:</b>My Laptop price is {{priceUsd | currency:'USD':true}}  
  5. </div>  
Output



Slice Pipe

Slice pipe is very similar to “limitto” filter in Angular 1.x but the order of the parameters is reversed. The first parameter of this pipe is start index and the second parameter is limit.

PipesExample.html
  1. <div>  
  2.     <h4>Slice Pipe Example</h4>  
  3.     <b>Without Slice:</b> {{description}}<br/>  
  4.     <b>With Slice:</b>{{description | slice:0:10}}...  
  5. </div>  
Output



Async Pipe

Angular 2 provides a new special pipe "Async". It allows us to bind the value of a variable directly with template which arrives asynchronously. It provides great ability for working with promises and observables.

App.component.ts
  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/PipesExample.html'  
  5. })  
  6. export class AppComponent {   
  7.     …  
  8.               ….  
  9.   
  10.                promise: Promise;  
  11.                constructor() {  
  12.                   this.promise = new Promise(function(resolve, reject) {  
  13.                     setTimeout(function() {  
  14.                       resolve("Hello, This text showing from promise.");  
  15.                     }, 2000)  
  16.                  });  
  17.     …  
  18.               ….  
  19.   }  
  20.   
  21. }  
PipesExample.html
  1. <div>  
  2.     <h4>Async Pipe Example</h4>  
  3.     {{ promise | async}}  
  4. </div>  
Output



In this example, after 2 seconds delay, the text will appear from the promise.

JSON Pipe

JSON pipe is very similar to “JSON” filter in Angular 1.x. It converts the JjavaScript object to JSON string and displays that on screen.

PipesExample.html
  1. <div>  
  2.     <h4>Json Pipe Example</h4>  
  3.     {{ testArray | json}}  
  4. </div>  
Output



Conclusion

Filters in AngularJS 1.x are known as Pipes in Angular 2. There are many built-in pipes supported by Angular 2. However, some of the filters of AngularJS 1.x like "filter" and "orderby" are not supported in Angular 2, due to performance issue. Instead of these filters, we can use component code for sorting and filtering the array results.


Similar Articles