Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

What is pipe in angular and create a custom pipe?

Aman Jain, September 2, 2021September 2, 2021

Pipes are used in angular to transform the data or value. Pipes can be able to transform any type of data type like string, date, currency or any data. It can be used both in template as well in component.

We can use pipes anywhere in our application and it required one time declaration in module.

Syntax of pipe:

We denotes pipe using symbol ‘|’.

Example:

{{ name | pipe_name }}

There is 6 types of in-built pipes in angular

  • DatePipe: Used for formatting the date.
  • UpperCasePipe: Used for transform the string into upper case.
  • LowerCasePipe: Used for transform the string into lower case.
  • CurrencyPipe: Used for a number to a currency string.
  • DecimalPipe: Used for Transforms a number into a string with a decimal point.
  • PercentPipe: Used for Transforms a number to a percentage string.

Example:

  • helloworld.html.ts
  • helloworld.component.ts
helloworld.html.ts
<p>My Name is {{name | uppercase }}
helloworld.component.ts
export class HelloWordComponent {
  name = 'devin'; // name is in lowe case
 
}

<p>The hero's birthday is {{ birthday | date }}</p>

We can also use two pipe simultaneously

<p>My Name is {{name | uppercase | lowercase }}

Parameterised pipe

Angular also support passing the parameters to pipe. let’s have an example of date pipe.

  • date.component.ts
  • date.compotent.html
date.component.ts
<p>Date is {{ datesobject | date:"MM/dd/yy" }} </p>
date.compotent.html
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'date-component',
  templateUrl: './date.component.html'
})
export class DateComponent {
  dateobject = new Date(1991, 4, 7); // April 7, 1991 -- since month parameter is zero-based
 }

How to create custom pipe in angular ?

Custom pipe is same as built-in pipe but we need to register and create their logic own.

Steps to create a pipe

  1. Create file anywhere in the project or create a specific folder for pipe and then create pipe in that folder ex. app\pipes\prettyname.pipe.ts.
  2. Import pipe import { Pipe, PipeTransform } from ‘@angular/core’;
  3. Create a decorator @Pipe
  4. Create a class and implement PipeTransform
  5. Implement the required method transform of PipeTransform
  6. Import Newly created pipe in module \app\app.module.ts

Or you can also create the pipe using ng generate

ng g pipe prettyname

Example:

  • prettyname.pipe.ts
  • helloworld.component.ts
  • app.module.ts
prettyname.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'prettyname'
})
export class PrettynamePipe implements PipeTransform {

  transform(value: string, ...args: unknown[]): string {
   
    return value.replace(new RegExp("_", 'g')," ").toUpperCase();
  }

}
helloworld.component.ts
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-helloworld',
  template: '{{name | prettyname}}', 
})
export class HelloworldComponent implements OnInit {
  name="test_with_bad_name";
   
  constructor() { }
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HelloworldComponent } from './helloworld/helloworld.component';

import { PrettynamePipe } from './prettyname.pipe';

@NgModule({
  declarations: [
    AppComponent,
    HelloworldComponent,
    PrettynamePipe
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Output in browser:

TEST WITH BAD NAME

In the above pipe prettyname we have transformed the string to pretty string.

Screenshot 2021 09 02 at 10.15.13 PM

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

Javascript How to Insert script tag dynamically in html in angular

How to Insert script tag dynamically in angular ?

March 24, 2022August 4, 2022

Sometimes we want to insert script tag in angular app, For an instance we have third party APIs or widget and we wanted to add it in our page then in this condition provided script are not angular friendly or we can say it can be pure JavaScript. so to…

Read More
Javascript how to install angular

What is Angular and how to install angular ?

August 29, 2021October 1, 2021

Angular is a framework of javascript. It’s used for creating efficient and single page application with ease. We can define the angular as follow: Angular is a component based component framework. Angular is collection of libraries which include routing, forms, validations, http requests etc. Angular is easy to use and…

Read More
Softwares disable change detection anagular

How to disable automatic change detection strategy in angular component?

July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version