Use pipes to transform strings, currency amounts, dates, and other data for display. Pipes are simple functions to use in template expressions to accept an input value and return a transformed value. Pipes are useful because you can use them throughout your application, while only declaring each pipe once.
Built-in pipes
Angular provides built-in pipes for typical data transformations, including transformations for internationalization (i18n), which use locale information to format data. The following are commonly used built-in pipes for data formatting:
DatePipe: Formats a date value according to locale rules.
UpperCasePipe: Transforms text to all upper case.
LowerCasePipe: Transforms text to all lower case.
CurrencyPipe: Transforms a number to a currency string, formatted according to locale rules.
DecimalPipe: Transforms a number into a string with a decimal point, formatted according to locale rules.
PercentPipe: Transforms a number to a percentage string, formatted according to locale rules.
AsyncPipe: Subscribe and unsubscribe to an asynchronous source such as an observable.
JsonPipe: Display a component object property to the screen as JSON for debugging.
To create a custom pipe, we have to import Pipe and Pipe Transform from Angular/core. In the @Pipe directive, we have captcha code javascript example to give the name to our pipe, which will be used in our .html file. Since, we are creating the sqrt pipe, we will name it sqrt.
As we proceed further, we have to create the class and the class name is SqrtPipe. This class will implement the PipeTransform.
What are pure Pipes?
These are pipelines that only employ pure functions. As a result, a pure pipe does not employ any internal state, and the output remains constant as long as the parameters provided remain constant. Angular calls the pipe only when the parameters being provided change. A single instance of the pure pipe is utilized in all components.
What are impure pipes?
Angular calls an impure pipe for each change detection cycle, independent of the change in the input fields. For each of these pipes, several pipe instances are produced. These pipes’ inputs can be altered.
By default, all pipelines are pure. However, as demonstrated below, you can specify impure pipes using the pure property.
Example:
@Pipe({
name: ‘impurePipe’,
pure: false/true
})
export class ImpurePipe {}
What is Pipe transform Interface in Angular?
An interface used by pipes to accomplish a transformation. Angular calls the transform function with the value of a binding as the first argument and any arguments as the second parameter in list form. This interface is used to implement custom pipes.
Example:
import { Pipe, PipeTransform } from ‘@angular/core’;
@Pipe({
name: ‘tranformpipe’
})
export class TranformpipePipe implements PipeTransform {
transform(value: unknown, …args: unknown[]): unknown {
return null;
}
}