Ad Code

✨🎆 JOIN MERN, JAVA, PYTHON, AI, DEVOPS, SALESFORCE Courses 🎆✨

Get 100% Placement Oriented Program CLICK to new more info click

What is Pipe in Angular | How to create custom pipe

What is Pipe in Angular | How to create custom pipe

Pipes are functions that take input values and return transformed output values inside Angular templates. You apply them using the | (pipe) operator in interpolation ({{ }}) or bindings.

<p>{{ today | date:'fullDate' }}</p>

<p>{{ name | uppercase }}</p>




In-built Pipe Name

PipeDescription
DatePipeFormats dates
UpperCasePipe / LowerCasePipeTransforms text case
CurrencyPipeFormats numbers as currency
DecimalPipeFormats numbers with decimal precision
PercentPipeFormats percentages
TitleCasePipeCapitalizes words
SlicePipeSlices arrays or strings
AsyncPipeUnwraps async values from Observable/Promise

Pipe Chaining:

To connect multiple files with each other is called Pipe Chaining.

{{ value | decimal:'1.0-2' | percent }}

Create Custom Pipe in Angular:

ng generate pipe reverse

ng g p reverse

it create reverse.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'reverse',
  standalone: true   // Angular 15+ recommended approach
})
export class ReversePipe implements PipeTransform {

  transform(value: string): string {
    if (!value) return '';
    return value.split('').reverse().join('');
  }

}

Connect pipe with components:

import { Component } from '@angular/core';
import { ReversePipe } from './reverse.pipe';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [ReversePipe],
  templateUrl: './app.component.html'
})
export class AppComponent {
  name = "Angular21";
}

in .html file
<h2>Original: {{ name }}</h2>
<h2>Reversed: {{ name | reverse }}</h2>





Post a Comment

0 Comments