Brief of Pipe

Diagrammatic representation of Pipe classification
Angular

Below, we will look into the above concepts one by one with an example.

Brief of Built-In Pipes

Syntax - Property value | uppercase

Simple Built-In Pipe example

In this example, we will use uppercase pipes to change the employee name from lowercase letters to uppercase letters.

FileName

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <table>
  9. <thead>
  10. <tr>
  11. <th>Code</th>
  12. <th>Name</th>
  13. <th>Gender</th>
  14. <th>Annual Salary</th>
  15. <th>Date of Birth</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <tr *ngFor='let employee of employees'>
  20. <td>{{employee.code}}</td>
  21. <td>{{employee.name|uppercase}}</td>
  22. <td>{{employee.gender}}</td>
  23. <td>{{employee.annualSalary}}</td>
  24. <td>{{employee.dateOfBirth}}</td>
  25. </tr>
  26. <tr *ngIf="!employees || employees.length==0">
  27. <td colspan="5">
  28. No employees to display
  29. </td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. </body>
  34. </html>

Filename - app.component.ts

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'my-app',
  4. templateUrl: 'app/app.component.html'
  5. })
  6. export class AppComponent {
  7. employees: any[] = [
  8. {
  9. code: 'emp101', name: 'karthik', gender: 'Male',
  10. annualSalary: 5500, dateOfBirth: '25/6/1988'
  11. },
  12. {
  13. code: 'emp102', name: 'sachin', gender: 'Male',
  14. annualSalary: 5700.95, dateOfBirth: '9/6/1982'
  15. },
  16. {
  17. code: 'emp103', name: 'rahul', gender: 'Male',
  18. annualSalary: 5900, dateOfBirth: '12/8/1979'
  19. },
  20. {
  21. code: 'emp104', name: 'mary', gender: 'Female',
  22. annualSalary: 6500.826, dateOfBirth: '14/10/1980'
  23. },
  24. ];
  25. }

Output

Before applying the uppercase pipe: just FYI

Angular
Same output, after applying the Uppercase pipe

Angular

Passing a parameter to Pipe

A Pipe can take any number of optional parameters to get the expected output.

Example

In the below code, we will see the complete usage of date pipes

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'my-app',
  4. template: `<div>
  5. Parameterizing a pipe using DatePipe <br>
  6. <p>---------------------------------------</p>
  7. Medium:-{{DOB | date:"medium"}}<br/>
  8. Short:-{{DOB | date:"short"}}<br/>
  9. Specific Date Format:-{{DOB | date:"dd/MM/yyyy"}}<br/>
  10. Short Time format:-{{DOB | date:"shortTime"}}<br/>
  11. Medium Time:-{{DOB | date:"mediumTime"}}<br/>
  12. Medium Date format:- {{DOB | date:"mediumDate"}}<br/>
  13. Full Date Format:- {{DOB | date:"fullDate"}}<br/>
  14. </div> `
  15. })
  16. export class AppComponent {
  17. DOB = new Date(1947, 8, 15)
  18. }

Output

Angular

Passing multiple parameters to Pipe

Let's use a currency example for this, which will be most frequently used in our projects.

Example

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'my-app',
  4. template: `<div>
  5. Passing Multiple Parameter to Pipe<br>
  6. <p>---------------------------------------</p>
  7. Annual Income :-{{salary | currency:'USD':true:'1.3-3'}}<br/>
  8. </div> `
  9. })
  10. export class AppComponent {
  11. salary: number = 2602132
  12. }

In the above code => currency:'USD': true:'1.3-3' is represent below,

Format syntax - {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits} => {1}.{3}-{3}

Output

Angular

Chain Pipes Example

By using the advantage of chaining, we can use two different pipes for the same property or column. Let us see how to use uppercase and date together in our example below.

Example

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'my-app',
  4. template: `<div>
  5. chaining a pipe <br>
  6. <p>---------------------------------------</p>
  7. Full Date Format:- {{DOB | date:"fullDate"|uppercase}}<br/>
  8. </div> `
  9. })
  10. export class AppComponent {
  11. DOB = new Date(1947, 8, 15)
  12. }

Output

Angular
Custom Pipes

To create a pipe in Angular 2, you have to apply the @Pipe decorator to class, which we can import from the core Angular library.

The @Pipe decorator allows you to define the pipe name that you'll use within template expressions.

Syntax

  1. import { Pipe, PipeTransform } from '@angular/core';
  2. @Pipe({ name: 'Pipename' })
  3. export class Pipeclass implements PipeTransform {
  4. transform(parameters): returntype { }
  5. }

Note

Transform() method will decide the input types, the number of arguments, and its types and output type of our custom pipe.

Custom Pipe Example

Note
To easily understand, we will use same list example which we used in the above concept, and additionally provide the title (Mr. or Miss.) to the employees as per their gender value.

Step 1

Create a file called employeeTitle.pipe.ts in the app folder. Add the code like below to that file.

File Name - employeeTitle.pipe.ts

  1. import { Pipe, PipeTransform } from '@angular/core';
  2. @Pipe({
  3. name: 'employeeTitle'
  4. })
  5. export class EmployeeTitlePipe implements PipeTransform {
  6. transform(value: string, gender: string): string {
  7. if (gender.toLowerCase() == "male")
  8. return "Mr." + value;
  9. else
  10. return "Miss." + value;
  11. }
  12. }

Step 2

Register this newly created class in your existing root module like below:

File Name-app.module.ts

Angular

Step 3

Add this line <td>{{employee.name | employeeTitle:employee.gender}}</td> to your existing HTML file called app.component.html.

File name - app.component.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <table>
  9. <thead>
  10. <tr>
  11. <th>Code</th>
  12. <th>Name</th>
  13. <th>Gender</th>
  14. <th>Annual Salary</th>
  15. <th>Date of Birth</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <tr *ngFor='let employee of employees'>
  20. <td>{{employee.code}}</td>
  21. <td>{{employee.name | employeeTitle:employee.gender}}</td>
  22. <td>{{employee.gender}}</td>
  23. <td>{{employee.annualSalary | currency:'USD':true:'1.3-3'}}</td>
  24. <td>{{employee.dateOfBirth}}</td>
  25. </tr>
  26. <tr *ngIf="!employees || employees.length==0">
  27. <td colspan="5">
  28. No employees to display
  29. </td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. </body>
  34. </html>

That’s it.

Final Output

Angular
Conclusion

Hope the above information will be very much helpful to implement pipes easily to our Angular project. Kindly let me know your thoughts or feedback. And, if you like to explore Angular concepts more, here are the links.