How to Develop @Pipes in Angular
Pipes
What is Pipes?
- Every application starts out with what seems like a simple task: get data, transform them, and show them to users. Getting data could be as simple as creating a local variable or as complex as streaming data over a WebSocket.
- A pipe takes in data as input and transforms it to the desired output. On this page, you'll use pipes to transform a component's birthday property into a human-friendly date.
- We can says about pipes - Pipe will help to you transform your value in desired output
Types of Pipes
- Built-in pipes : - These pipes are in build pipes comes with Angular. You can use them in any template e.g. DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, and PercentPipe
- Parameterizing a pipe : - These pipes can accept any optional parameter in order to transform your value in desired output
- Chaining pipes :- You can chain pipes together in order to use usefull combination
- Custom pipes : - Angular gives us the flexibility to create our own pipes based on your requirements. Whenever we create our own pipes i.e. is known as custom pipes.
Let's create a custom pipe: We need to convert the first character should get converted uppercase using pipes
- Step 1 : Create a new Project using command : ng new custom-pipe-example
- Step 2 :create a pipe in same project using command: ng generate pipe myPipe
-
Step 3 :Place below code in app.component.html
Pipe Example - Angular
Enter Your Name Your name (After transformation using Pipe) : {{name | mypipe}} -
Step 4 :Place below code in app.component.ts
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent { title = 'pipe-demo'; name: string; } -
Step 5 : open file mypipe.pipe.ts and replace below code with transform method
transform(value: string, ...args: unknown[]): string { if (value) { return value.charAt(0).toUpperCase() + value.substring(1, value.length); } - Step 6 : hit ng serve command. You are ready to use your pipes
Comments
Post a Comment