Learn Angular - Pipes In Angular and How To Create Custom Pipes in Angular
Filters in Angular JS are known as Pipes in Angular.
Pipes in Angular are used to transform the output data before displaying to the web page same as filters are used in Angular JS. We need to use the pipe character “|” to apply on property.
Angular introduced a lot of built-in pipes like –
- Date Pipe
- Uppercase Pipe
- Lowercase Pipe
- Currency Pipe & many more
Let’s take the same “student” component examples which I have been using so far in my previousarticles.
Date Pipe
Date Pipe in Angular is used for transforming the output date before displaying. For example, we have the below student list and we want to apply the date pipe on DOB.
- { studentID: 1, studentName: 'Steve', gender: 'Male', age: 35, course: 'MCA', DOB: '10/12/1982', grade: 0.7500, rating: 7.5123 },
- { studentID: 2, studentName: 'Bobby', gender: 'Male', age: 32, course: 'MBA', DOB: '12/1/1985', grade: 0.7850, rating: 7.8223 },
- { studentID: 3, studentName: 'Rina', gender: 'Female', age: 45, course: 'B.Tech', DOB: '9/11/1972', grade: 0.8525, rating: 8.5263 },
- { studentID: 4, studentName: 'Alex', gender: 'Female', age: 24, course: 'M.Tech', DOB: '1/1/1993', grade: 0.5540, rating: 5.5123 },
- { studentID: 5, studentName: 'Rahul', gender: 'Male', age: 26, course: 'MCA', DOB: '1/21/1991', grade: 0.9550, rating: 9.5534 },
To apply date pipe, use the following command.
td>{{s.DOB | date }}</td>
Output
Date Pipe with parameters
Date Pipe with parameters
A pipe can accept any number of optional parameters to format its output. Suppose we want to format date here, we can use parameter here using colon “:” character.
Example 1.
<td>{{s.DOB | date:'MM/dd/y' }}</td>
Output
Example 2.
Example 2.
<td>{{s.DOB | date:'yMMMMd' }}</td>
Output
Uppercase & Lowercase Pipe
Uppercase & Lowercase Pipe
Uppercase & Lowercase pipes are used to transform the text output into uppercase and lowercase respectively. To transform “Student Name” into uppercase letters and “Gender” in lowercase letters, we use the following command.
- <td>{{s.studentName | uppercase}}</td>
- <td>{{s.gender | lowercase}}</td>
Output
Percent & Decimal Pipes
Percent & Decimal Pipes
The Percent and Decimal pipes are newly introduced in Angular. These both take a parameter to describe how many integer and fraction digits the number should be formatted with. The parameter we pass for formatting follows this pattern –
Property | { minIntegerDigits }.{ minFractionDigits } - { maxFractionDigits }
The decimal pipe is accessed using Number as shown below –
Student.component.html
- <table>
- <thead>
- <tr>
- <th>Index</th>
- <th>Student ID</th>
- <th>Name</th>
- <th>Gender</th>
- <th>Age</th>
- <th>Course</th>
- <th>DOB</th>
- <th>Grade</th>
- <th>Rating</th>
- </tr>
- </thead>
- <tbody>
- <tr *ngFor="let s of students;">
- <td>{{i}}</td>
- <td>{{s.studentID}}</td>
- <td>{{s.studentName | uppercase}}</td>
- <td>{{s.gender | lowercase}}</td>
- <td>{{s.age}}</td>
- <td>{{s.course}}</td>
- <td>{{s.DOB | date:'yMMMMd' }}</td>
- <td>{{s.grade | percent:'.2'}}</td>
- <td>{{s.rating | number:'2.1-2'}}</td> // Pipe with multi-parameters
- </tr>
- </tbody>
- </table>
Output
Pipes Chaining in Angular 2
Pipes Chaining in Angular 2
We can chain pipes together by using multiple pipes, like below.
- <td>{{s.DOB | date:'yMMMMd' | uppercase }}</td>
Output
Custom Pipes in Angular 2
Custom Pipes in Angular 2
In Angular 2, we can create custom pipes to transform our data according to us. Let’s understand this with the help of below example.
Suppose we have student records as shown in table format. And we want to categorize students into Graduation or Post-graduation categories according to their course. That means - we want to transform the Course column data like below.
Let’s understand with the following steps how we can achieve this with the help of custom pipe –
- Let’s create a separate TypeScript class file named “coursepipe.ts” to create the custom pipe “studentCoursePipe”. Copy the following code in the file.
student.coursepipe.ts- import {Pipe, PipeTransform} from '@angular/core'
- @Pipe({
- name:'courseCategory'
- })
- export class courseCategoryPipe implements PipeTransform {
- transform(value: string):string
- {
- if (value.toLowerCase() == "mca" || value.toLowerCase() == "mba" || value.toLowerCase() == "m.tech") {
- return value + " : Post-Graduation";
- }
- else if (value.toLowerCase() == "b.tech") {
- return value + " : Graduation";
- }
- }}
- Register “courseCategoryPipe” in the Angular app module to use. In the module.ts file, import “courseCategoryPipe” and include it in the declaration like below highlighted code.
app.module.ts- import { NgModule } from '@angular/core';
- import { BrowserModule } from '@angular/platform-browser';
- import { AppComponent } from './app.component';
- import { StudentListComponent } from './student/student.component'
- import { courseCategoryPipe } from './student/student.coursepipe'
- @NgModule({
- imports: [BrowserModule],
- declarations: [AppComponent, StudentListComponent, courseCategoryPipe],
- bootstrap: [ AppComponent ]
- })
- export class AppModule { }
- In the component.html file, use courseCategoryPipe as highlighted in below code. We need not to pass course property value as parameter. It will be passed automatically.
student.component.html- <table>
- <thead>
- <tr>
- <th>Index</th>
- <th>Student ID</th>
- <th>Name</th>
- <th>Gender</th>
- <th>Age</th>
- <th>Course</th>
- <th>DOB</th>
- <th>Grade</th>
- <th>Rating</th>
- </tr>
- </thead>
- <tbody>
- <tr *ngFor="let s of students;">
- <td>{{i}}</td>
- <td>{{s.studentID}}</td>
- <td>{{s.studentName | uppercase}}</td>
- <td>{{s.gender | lowercase}}</td>
- <td>{{s.age}}</td>
- <td>{{s.course | courseCategory}}</td> // Custom pipe used here
- <td>{{s.DOB | date:'yMMMMd' | uppercase }}</td>
- <td>{{s.grade | percent:'.2'}}</td>
- <td>{{s.rating | number:'2.1-2'}}</td>
- </tr>
- </tbody>
- </table>
- <br />
- <button (click)='getStudents()'>Get More Students</button>
Output
Summary
Summary
In this article, we covered the following topics –
- What are pipes in Angular2?
- Built-in pipes introduced in Angular2.
- Pipe with parameters
- Pipe chaining
- Custom pipes in Angular 2
Write to me in the comment box in case you need any help or you have any questions or concerns. Have a good day!
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge
ReplyDeleteAngular JS Online training
Angular JS training in Hyderabad