Pipes In Angular

Introduction

Pipes in Angular allow us to display the data in the view after some transformation. It can be applied to different properties like string, integer, date, currency etc.

Prerequisites

Let us create a sample TestApp; for this, you should have installed the below for the development environment:

  1. Node
  2. Npm (comes when you install node)
  3. Angular CLI.
  4. Text Editor.

For creating a new application run the below command on your location.

> ng new TestApp

Create a new component.

> ng g c test

Once your command is completed you will have a TestApp folder created inside your sample folder.

Now, you will have your project folder called 'TestApp'.

Note
See my previous article “Getting started with Angular CLI” If you want the installation and introduction from the basics, start with the execution of the sample application.

Let us start with string operations pipes,

Open test.component.ts and add the below contents,

  1. import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   public myName1 = "mohammad irshad";  
  10.   public myName2 = "MOHAMMAD IRSHAD";  
  11.   
  12.   constructor() {  
  13.    }  
  14.   
  15.   ngOnInit() {  
  16.   }  
  17. }  

Open test.component.html and add the below contents,

  1. <h2>This is Test Component!</h2>  
  2.   
  3. <h3>Lower case: {{myName1}}</h3>  
  4. <h3>Upper case: {{myName2}}</h3>  
  5.   
  6. <h3>Convert Lower to upper: {{myName1 | uppercase}}</h3>  
  7. <h3>Convert Upper to Lower: {{myName1 | lowercase}}</h3>  

Run the applications,

Pipes In Angular 

Other string pipes like titlecase, slice, JSON available builtin. Titlecase allows us to write string where the first character of all the words are in uppercase and remaining ones are in lower case. Slice pipe is used to get the string based on the index defined, JSON allows us to write the data in the form of the JSON with brackets inside of the key-value pair.

Transforming number value with pipe

Open test.component.html and add the below contents,

  1. <h2>This is Test Component!</h2>  
  2. <h3>Number pipe in angular.</h3>  
  3. <h3>{{1.23456 | number : '1.3-4'}}</h3>  
  4. <h3>{{5.54545454 | number : '3.1-2'}}</h3>  

Save and run the application,

Pipes In Angular
  • n.p-q - where ‘n’ defines the minimum number of digits that will display before the decimal point. ‘p’ defines thee minimum digits after the decimal and ‘q’ defines the max digits after the decimal.
  • percent pipe - allows us to write the number with percent at the end.
  • Currency pipe - allows us to write the currency formats with the help of its code.
  • Date pipes - Date pipe allow us to write the date in a different format like in short date, only date etc.

Open test.component.ts and add the below property,

  1. public date = new Date();  

open test.component.html and add the below contents,

  1. <h2>This is Test Component!</h2>  
  2. <h3>no pipe applied : {{date}}</h3>  
  3. <h3>short date format: {{date | date : 'short'}}</h3>  
  4. <h3>only date : {{date | date :'shortDate'}}</h3>  
  5. <h3>only time : {{date | date :'shortTime'}}</h3>  

Save and run the application,

Pipes In Angular 

Custom pipes in Angular

Let us add a custom pipe that will take string to transform before sending it to view. We will print the name if the name value is blank then it will display ‘no name’, if available then it will print the name string normally.

Open app.module.ts and add the below contents,

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule, Pipe } from '@angular/core';  
  3. import {FormsModule} from '@angular/forms';  
  4. import { AppComponent } from './app.component';  
  5. import { TestComponent } from './test/test.component';  
  6.   
  7. @Pipe({  
  8.   name: "namePipe"  
  9. })  
  10.   
  11. class NamePipe{  
  12.   transform(value : string, defaultValue : string) : string{  
  13.     if(value != ""){  
  14.       return value;  
  15.     } else {  
  16.       return defaultValue;  
  17.     }  
  18.   }  
  19. }  
  20.   
  21. @NgModule({  
  22.   declarations: [  
  23.     AppComponent,  
  24.     TestComponent,  
  25.     NamePipe  
  26.   ],  
  27.   imports: [  
  28.     BrowserModule,  
  29.     FormsModule  
  30.   ],  
  31.   providers: [],  
  32.   bootstrap: [AppComponent]  
  33. })  
  34. export class AppModule { }  

Open test.component.ts and add the below contents,

  1. import { Component, OnInit, Pipe } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-test',  
  5.   templateUrl: './test.component.html',  
  6.   styleUrls: ['./test.component.css']  
  7. })  
  8. export class TestComponent implements OnInit {  
  9.   
  10.   public name="Mohammad Irshad";  
  11.   public blankValueName = "";  
  12.   public defaultName = "No Name.";  
  13.   constructor() {  
  14.    }  
  15.   ngOnInit() {  
  16.   }  
  17. }  

Open test.component.html and add the below contents,

  1. <h2>This is Test Component!</h2>  
  2. <h3>Custom pipe: </h3>  
  3. <h3>{{name | namePipe : defaultName}}</h3>  
  4. <h3>{{blankValueName | namePipe : defaultName}}</h3>  

Save and run the application,

Pipes In Angular 

In this way you can add any logic that will perform the transformation of data with the help of default available pipes given by Angular or you can go with your custom pipes.