Angular is a huge framework we can make almost everything possible in the website nowadays, it provides a number of features where we can make thing according to our requirements.
With angular we can have progressive web apps , server side rendering and other rich functionalities that are really difficult in other platforms now.
Today we are going to focus on pipes in Angular where we can format or transform almost every kind of data or data types.
Angular itself provides a various types of pipes where it is easy to map dates, currencies, strings etc but what if we have a custom requirement on making an array ,string and number to be mapped or formatted as we want ? Now we have custom pipes in the game to make things done.
First learn how many types of pipe are there.
Types of pipe in angular
There are two types of pipe in angular
Pure Pipes
When angular finds any change in value or the parameter passed in pipe then it is called pure, when we set pure to true under pipe decorator then it is true.
Angular pipes are pure by default. we can cache the result of previous value or input when set to true. input properties should not be mutable in this case. pure pipes look for pure change if the input is changed weather it can be string, number, Boolean, date, array and objects.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name:'filter',
pure: true
})
export class FilterPipe implements PipeTransform{
transform(value: any, name: string) {
if(name === ''){
return value;
}
return value.filter((user) => user.firstName.startsWith(name));
}
}
Impure Pipes
@Pipe({
name:'filter',
pure: false
})
Custom pipes in Angular
Steps to create custom pipe in angular
- Give a meaningful name for custom pipe.
- create a file with extension (.pipe.ts) e.g. custom.pipe.ts
- Import Pipe and PipeTransform from @angular/core.
- Add a Class named CustomPipe.
- Add a decorator named pipe e.g. @pipe({})
- The custom pipe class should implement PipeTransform interface.
- now implement pipe transform method that accepts value agrument.
- Add this pipe to the module you want to use the pipe in component of e.g. app.module.ts file.
- add pipe in the declaration
- now use the custom pipe in interpolation e.g. {{ name | 'custom' }}
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'SquarePipe'
})
export class SqaureNumPipe implements PipeTransform {
transform(value: number) {
return value * value
}
}
Declare it in app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SquareNumPipe } from './square-num.pipe';
@NgModule({
declarations: [
AppComponent,
SquareNumPipe
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Finally we are ready to use our custom pipe that returns the square of input value
{{ inputValue | SquarePipe }}
0 Comments