This article covers following topics in details:
- Purpose of having pipes in Angular.
- Built in Pipes and its usage.
- How to create custom Pipes
{{ amount | currency:'EUR' }}- Uppercase pipe
- Lowercase pipe
- Date pipe
- Currency pipe
- Percent pipe
- Decimal pipe
- Slice pipe
ng new angular-pipesimport { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss']})export class AppComponent { title = 'angular-pipes'; todayNumber: number = Date.now(); todayDate : Date = new Date(); todayString : string = new Date().toDateString(); todayISOString : string = new Date().toISOString(); amount = 1000; floatValues = 2.5; number1 = 13.456789; week=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; arrayToTranform = [1000,1500,5000];}<h1>datepipe for milisecond value: {{todayNumber}}</h1> <h2> {{todayNumber | date}} </h2>
<h1>datepipe for date Object: {{todayDate}}</h1> <h2> {{todayDate | date}} </h2>
<h1>datepipe for date String: {{todayString}}</h1> <h2> {{todayString | date}} </h2> <h1>datepipe for ISO date String: {{todayISOString}}</h1> <h2> {{todayISOString | date}} </h2>{{todayDate | date :'short'}}<h1>Amount {{amount}} in USD format</h1> <h2> {{amount | currency}} </h2>
<h1>Amount {{amount}} in Japanese yen format</h1>
<h2> {{ amount | currency:'JPY' }} </h2><h1>Amount {{floatValues}} as a percentage</h1> <h2> {{floatValues | percent}} </h2>
<h1>Amount {{floatValues}} as a percentage</h1> <h2> {{floatValues | percent:'2.2-5'}} </h2>{{floatValues | percent:'2.2-5'}}<h1>Amount {{number1}} as a decimal value</h1> <h2> {{number1 | number}} </h2>
<h1>Amount {{number1}} as a decimal value</h1> <h2> {{number1 | number:'2.2-5'}} </h2>{{number1 | number:'2.2-5'}}week=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]<h1>All days in a week</h1> <div *ngFor="let day of week"> {{day}} </div>
<h1>First 3 day in a week</h1> <div *ngFor="let day of week|slice:0:3"> {{day}} </div>import { Pipe } from '@angular/core';
@Pipe({ name: 'format-number'})export class FormatNumberPipe{ }import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';import { AppComponent } from './app.component';import { FormatNumberPipe } from './pipe/formatNumber';
@NgModule({ declarations: [ AppComponent, FormatNumberPipe ], imports: [ BrowserModule, AppRoutingModule, ], providers: [], bootstrap: [AppComponent]})export class AppModule { }import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'formatNumber'})export class FormatNumberPipe implements PipeTransform{ transform(n: number) : string { return (n /1000)+"K"; }}arrayToTranform = [1000,1500,5000];<h1>Before applying custom pipe</h1> <div *ngFor = "let number of arrayToTranform "> {{number}} </div>
<h1>After applying custom pipe</h1> <div *ngFor = "let number of arrayToTranform"> {{number | formatNumber}} </div>












