Getting Started with Pipes in Angular

What are Pipes in Angular?

Pipes are simply a function that we can directly apply on any expression/value in a template to transform it into some other value.

The value on which we are applying the pipe is the input for our pipe function. The value returned from that function will be replaced in place original value.

Built-in pipes in Angular

Angular comes with many built-in pipes. Some of them include:

  • uppercase (to convert string in upper case);
  • lowercase (to convert string in upper case);
  • date (to format the date into different types);
  • json ( to convert a value or object into JSON formatted string).

Examples of pipes in use:

<p>{{ 'Makesh' | uppercase }}</p>
<p>{{ 'Kumar' | lowercase }}</p>
<p>{{ { name: 'makesh' } | json }}</p>

Passing arguments to pipes

Pipes like date, currency will take arguments for pipe function, to pass argument follow the pipe name (currency) with a colon (:) and the parameter value (‘EUR’).

For example:

<p>{{ 100 | currency: 'INR' }}</p>
<p>{{ currentDate | date: 'dd/MM/yyyy' }}</p>

Chaining multiple pipes

To chain multiple pipes together, we just have to use a pipe operator (|) between different pipe names, see the below code for example

<p>{{ currentDate | date | uppercase}}</p>

Types of pipes

Pipes can be classified into:

  1. Pure Pipes
  2. Impure Pipes

1. Pure Pipes

A pure pipe is only called when Angular detects a change in the value or the parameters passed to a pipe.

2. Impure Pipes

An impure pipe is called for every change detection cycle no matter whether the value or parameter(s) changes.

Creating our own custom pipes

To create our own custom pipe, follow the below steps:

  • Create a class and apply @ Pipe decorator (without the space between @ and Pipe) to it and pass an object to the decorator which contains two properties:
    name (name of the pipe)
    pure (the optional property which determines the type of pipe, by default it is true, we can provide value as false to make pipe impure).
  • Register the class in the respective module declarations array.
  • Implement the PipeTransform interface in your custom pipe class to perform the transformation.
  • PipeTransform interface has a function called transform which we have to implement in our class, transform is the function that is going to have our logic to transform values.
  • the first parameter of the transform function holds the value on which the is pipe is applied and the remaining parameters are the values which is passed while invoking the pipe (using : operator).
    after performing transformation as per our logic we have to return the transformed value.
  • Now we can apply the pipe in our template by using the name of our pipe with the pipe (|) operator
example for custom pipe

See a live example in stackblitz

Read more on Angular Pipes

Thank you for reading. I hope you have found this useful. Happy learning!

Build modular, reusable React components with Bit.

Monorepos are a great way to speed up and scale app development, with independent deployments, decoupled codebases, and autonomous teams.

Bit offers a great developer experience for building component-driven monorepos. Build components, collaborate, and compose applications that scale. Our GitHub has over 14.5k stars!

Give Bit a try →

An independently source-controlled and shared “card” component (on the right, its dependency graph, auto-generated by Bit)

Learn more


Getting Started with Pipes in Angular was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Makesh Kumar

What are Pipes in Angular?

Pipes are simply a function that we can directly apply on any expression/value in a template to transform it into some other value.

The value on which we are applying the pipe is the input for our pipe function. The value returned from that function will be replaced in place original value.

Built-in pipes in Angular

Angular comes with many built-in pipes. Some of them include:

  • uppercase (to convert string in upper case);
  • lowercase (to convert string in upper case);
  • date (to format the date into different types);
  • json ( to convert a value or object into JSON formatted string).

Examples of pipes in use:

<p>{{ 'Makesh' | uppercase }}</p>
<p>{{ 'Kumar' | lowercase }}</p>
<p>{{ { name: 'makesh' } | json }}</p>

Passing arguments to pipes

Pipes like date, currency will take arguments for pipe function, to pass argument follow the pipe name (currency) with a colon (:) and the parameter value (‘EUR’).

For example:

<p>{{ 100 | currency: 'INR' }}</p>
<p>{{ currentDate | date: 'dd/MM/yyyy' }}</p>

Chaining multiple pipes

To chain multiple pipes together, we just have to use a pipe operator (|) between different pipe names, see the below code for example

<p>{{ currentDate | date | uppercase}}</p>

Types of pipes

Pipes can be classified into:

  1. Pure Pipes
  2. Impure Pipes

1. Pure Pipes

A pure pipe is only called when Angular detects a change in the value or the parameters passed to a pipe.

2. Impure Pipes

An impure pipe is called for every change detection cycle no matter whether the value or parameter(s) changes.

Creating our own custom pipes

To create our own custom pipe, follow the below steps:

  • Create a class and apply @ Pipe decorator (without the space between @ and Pipe) to it and pass an object to the decorator which contains two properties:
    - name (name of the pipe)
    - pure (the optional property which determines the type of pipe, by default it is true, we can provide value as false to make pipe impure).
  • Register the class in the respective module declarations array.
  • Implement the PipeTransform interface in your custom pipe class to perform the transformation.
  • PipeTransform interface has a function called transform which we have to implement in our class, transform is the function that is going to have our logic to transform values.
  • the first parameter of the transform function holds the value on which the is pipe is applied and the remaining parameters are the values which is passed while invoking the pipe (using : operator).
    after performing transformation as per our logic we have to return the transformed value.
  • Now we can apply the pipe in our template by using the name of our pipe with the pipe (|) operator
example for custom pipe

See a live example in stackblitz

Read more on Angular Pipes

Thank you for reading. I hope you have found this useful. Happy learning!

Build modular, reusable React components with Bit.

Monorepos are a great way to speed up and scale app development, with independent deployments, decoupled codebases, and autonomous teams.

Bit offers a great developer experience for building component-driven monorepos. Build components, collaborate, and compose applications that scale. Our GitHub has over 14.5k stars!

Give Bit a try →

An independently source-controlled and shared “card” component (on the right, its dependency graph, auto-generated by Bit)

Learn more


Getting Started with Pipes in Angular was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Makesh Kumar


Print Share Comment Cite Upload Translate Updates
APA

Makesh Kumar | Sciencx (2022-01-13T18:00:29+00:00) Getting Started with Pipes in Angular. Retrieved from https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/

MLA
" » Getting Started with Pipes in Angular." Makesh Kumar | Sciencx - Thursday January 13, 2022, https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/
HARVARD
Makesh Kumar | Sciencx Thursday January 13, 2022 » Getting Started with Pipes in Angular., viewed ,<https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/>
VANCOUVER
Makesh Kumar | Sciencx - » Getting Started with Pipes in Angular. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/
CHICAGO
" » Getting Started with Pipes in Angular." Makesh Kumar | Sciencx - Accessed . https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/
IEEE
" » Getting Started with Pipes in Angular." Makesh Kumar | Sciencx [Online]. Available: https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/. [Accessed: ]
rf:citation
» Getting Started with Pipes in Angular | Makesh Kumar | Sciencx | https://www.scien.cx/2022/01/13/getting-started-with-pipes-in-angular/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.