How to Use Pipe In Angular Types of Pipe.

What are pipes and how we can use pipe in Angular?

In each application, we get the data from database and display it on our View, but more often, we have to format or transform the data before displaying it. For this, we use PIPES. For example, database provides the date in the format “Fri Apr 15 1988 00:00:00 GMT-0700” but we want to show it in the View as April 15, 1988, for this we use pipes.
OR
Pipes transform the displayed values in a template. Use the @Pipe annotation to declare that a particular class is a pipe. A pipe class must also implement a PipeTransform interface. The @Pipe decorator allows you to define the name of the pipe that is globally available for use in any template across Angular application. The Pipe class implements the transformation method of “PipeTransform” interfaces which accepts an input value and returns the transformed result.
There will be one additional argument on the transform method for each parameter passed to the pipe.
The CLI command to generate Pipe:

What is the differences between Angular Js and Angular 2

ng g p pipename

or

ng generate pipe pipename

Pipe decorator and metadata are:

The pipe name is used for template binding.
To use the pipe, you must set a reference to this pipe class in the module.

Why we use Pipes?

Sometimes, the data is not displayed in the well format in the HTML templates. We can run a function in the HTML template to get its returned value.
For example – If you want to see a credit card number on your web apps you can not display the whole number on your web app , you should write a custom logic to display card number as like
*-21475 using the custom pipe.

some built-in pipe provided by the Angular

Currency
Date
Decimal
LowerCase
UpperCase
Percent

Slice
Async

create custom pipe in Angular

in Angular we have some built-in pipes, but you can also build your own pipes. A pipe accepts the values and then returns a value after transformation based on the filter criteria that will be added in the TypeScript (.ts) file. The @Pipe decorator allows you to define the name of the pipe that will be used within the
template expressions. It must be a valid JavaScript identifier.

Angular Form Array – Step by Step Example

Types of pipes are support by Angular

There are two types of pipes which angular support:-

Pure Pipes: Angular only executes pure pipe when it detects a pure change in the input value. A pure change is either a change to a primitive input value like String, Number, Boolean, Symbol or a changed object reference like Date, Array, Function, Object.
Impure Pipes: Angular executes an impure pipe when change is detected in every component. An impure pipe is often called, whenever a key or mouse movement occurs

Leave a Reply

Your email address will not be published. Required fields are marked *