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 Dynamic Autocomplete Using Typehead JS

Laravel Dynamic Autocomplete Using Typehead JS

July 16, 2022July 16, 2022

In this article we will learn to use Laravel Dynamic Autocomplete Using Typehead JS. Typehead JS is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel…

Read More

How to make a reactive form in angular?

August 29, 2021September 10, 2021

Angular has vast library of modules so reactive form is one of the part of angular library. Prerequisites knowledge: Angular HTML CSS Javascript So, let’s start building a from . Steps to create a reactive form in angular Create a new or existing angular project, you can also read this…

Read More
Javascript Password and confirm password validation in javascript

Password and confirm password validation in javascript

September 17, 2021November 21, 2021

In this tutorial, we will cover password strength validation , password not empty validation and confirm password validation. we will check on every submission of form whether the entered password pass or fail the criteria. Password Strength validation in javascript Password field must contain At least 8 characters Should be…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version