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. @Component({
  3. selector: 'app-test',
  4. templateUrl: './test.component.html',
  5. styleUrls: ['./test.component.css']
  6. })
  7. export class TestComponent implements OnInit {
  8. public myName1 = "mohammad irshad";
  9. public myName2 = "MOHAMMAD IRSHAD";
  10. constructor() {
  11. }
  12. ngOnInit() {
  13. }
  14. }

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

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

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. @Pipe({
  7. name: "namePipe"
  8. })
  9. class NamePipe{
  10. transform(value : string, defaultValue : string) : string{
  11. if(value != ""){
  12. return value;
  13. } else {
  14. return defaultValue;
  15. }
  16. }
  17. }
  18. @NgModule({
  19. declarations: [
  20. AppComponent,
  21. TestComponent,
  22. NamePipe
  23. ],
  24. imports: [
  25. BrowserModule,
  26. FormsModule
  27. ],
  28. providers: [],
  29. bootstrap: [AppComponent]
  30. })
  31. export class AppModule { }

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

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

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.