pipe in angular code example
Example 1: angular number pipe
//{{ value_expression | number [ : digitsInfo [ : locale ] ] }}
//Assume someNumber = 12.221
<p>{{ someNumber | number : '1.0-0'}}</p>
//The output here is '12' because we cut the decimals off
//with the number pipe
//More examples from Angular:
//Assume:
//pi: number = 3.14;
//e: number = 2.718281828459045;
<p>e (no formatting): {{e | number}}</p>
<p>e (3.1-5): {{e | number:'3.1-5'}}</p>
<p>e (4.5-5): {{e | number:'4.5-5'}}</p>
<p>e (french): {{e | number:'4.5-5':'fr'}}</p>
<p>pi (no formatting): {{pi | number}}</p>
<p>pi (3.1-5): {{pi | number:'3.1-5'}}</p>
<p>pi (3.5-5): {{pi | number:'3.5-5'}}</p>
<p>-2.5 (1.0-0): {{-2.5 | number:'1.0-0'}}</p>
//More information here: https://angular.io/api/common/DecimalPipe
Example 2: command to create custom pipe in angular 6
ng generate pipe personName
Example 3: applying datepipe on interepolating date in html from typescipt
tooltip="{{recentDate | date: 'medium'}}" // Using interpolation
Example 4: angular pipes
//component.ts file:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe ({
name : 'sqrt'
})
export class SqrtPipe implements PipeTransform {
transform(val : number) : number {
return Math.sqrt(val);
}
}
//import SqrtPipe inside your module.ts ,
import { SqrtPipe } from './app.sqrt';
declarations: [
SqrtPipe,
],
//then you can use this pipe in component.html file.
// <h2>Square root of 625 is: {{625 | sqrt}}</h2>