A Brief Introduction of Pipe in Angular

Posted By : Damandeep Singh | 31-May-2018

In Angularjs and Pipes were  known as Filters.In Angular 2 it is named  as Pipe. the | character is used to transform data.

Syntax :

{{ Welcome to Angular 4 | lowercase}}

It takes integers, strings, arrays and date as input seperated with | to be converted in the format as required.

Angular 4 provides some built in pipes :

  1. Lowercasepipe
  2. Uppercasepipe
  3. Datepipe
  4. Currencypipe
  5. Jsonpipe
  6. Percentpipe
  7. Decimalpipe
  8. Slicepipe

Angular also allwos to make custom Pipes

  • To create a custom pipe, create a new TS file
  • import pipe and pipe transform from @angular/core.
  • give the name to our pipe under @pipe directive which will be used in our .html file
  • implement PipeTransform with class
  • Define transform method which will return your expected result
  • Import it in app.module.ts file and declare in declarations

example :

import {Pipe, PipeTransform} from '@angular/core';
@Pipe ({
   name : 'sqrt'
})
export class SqrtPipe implements PipeTransform {
   transform(val : number) : number {
      return Math.sqrt(val);
   }
}

About Author

Author Image
Damandeep Singh

Damandeep Singh is a Front-end Developer having Good knowledge of HTML, CSS3/CSS, Bootstrap, Angular, Ionic. He likes to listen to Music in Free time.

Request for Proposal

Name is required

Comment is required

Sending message..