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

Uncategorized update page data without reload

How to update page data without reload in jQuery?

October 24, 2021November 17, 2023

If you are beginner then this post is for you. For showing the new content on page without refresh we will use JQuery Reload Page without Refresh and server side PHP. In this article, I will demonstrate to show the latest content from server without refreshing the page. This is…

Read More
Softwares angular file upload

How to upload multiple files in angular 12 reactive form

June 20, 2021November 8, 2023

How to upload multiple files in angular 12 reactive form Uploading multiple files with progress in angular is too easy, you can implement it in your website using the below guideline. i will suggest to read my last post Angular file upload with post form data and php $_FILES to…

Read More
Javascript Password and confirm password validation in angular

Password and confirm password validation in angular 14 ?

September 9, 2022March 16, 2024

Angular 14 provides built-in library for validation but it doesn’t have built in validation for password and confirm password. Angular 14 has release and we are excited to work with new feature so i am also updating the most search article with latest version of angular. so in this tutorial…

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

  • 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 Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version