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 Laravel Ajax Autocomplete Using Select2

Laravel Ajax Autocomplete Using Select2

July 17, 2022July 17, 2022

In this article we will learn to use Laravel Ajax Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and when…

Read More
Javascript Laravel Ajax Autocomplete Using Select2

Laravel Customized Autocomplete Options Using Select2

July 18, 2022July 18, 2022

In this article we will learn to use Laravel Customized Autocomplete Options Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and…

Read More
Javascript How to get query string params in angular 12

How to get query string params in angular 12 ?

April 6, 2022October 4, 2022

In this tutorial i will show you to get the query string params in angular using ActivatedRoute. Angular completely works in different way from other javascript frameworks, Angular uses its own routing framework to land between the pages. Let’s quickly deep dive in to the topic and learn it. So…

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

  • 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
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version