Introduction
In this article, we will be discussing Pipes in Angular 5. Pipes are the operators that are used to format the data in Angular. This comes from the symbol (|) used to do this operation. Pipes are there in Angular from Angular v2 onwards. But it has evolved over the period of time. There are many inbuilt pipes available in Angular 5 and it gives an easy template to create your own pipe using @angular/cli.
Newbies to Angular 5 should check Hello world with Angular5 before moving to this topic.
All the built-in pipes are available in @angular/common npm package under CommonModule.
Inbuilt Pipes in Angular5
Before jumping into the custom pipes in Angular, let's first discuss the inbuilt pipes of Angular.
String pipes
| UpperCase | Converts the string type data to upper case in the UI. |
| lowercase | Converts the string type data to lower case in the UI. |
| titlecase | Converts the string type data in which the first alphabet of each word is made capital and the rest will be in the small case. |
| Slice : <starting index> | It works on string fields or Array. It slices the input from the index till the end of the string or array length. |
| Slice : <starting index>:<end index> | It works on string fields or array. It slices the input from the starting index to the end index. (excluding the end index) |
Code for the same:
HTML part
- <h2>string pipes</h2>
- <h3>{{authorPosition | uppercase}} : {{authorName}}</h3>
- <h3>Article Name : {{articleName | titlecase}}</h3>
- <br>
- <h4>slice demo</h4>
- <h4>{{articleName | slice :5 }}</h4>
- <h4>{{articleName | slice :5:8 }}</h4>
Typescript part
- this.authorPosition = "blogger";
- this.authorName = "Biswabid";
- this.articleName = "pipes in angular";
output

Date pipes
There are many date type pipes available in Angular. Following are some of the important ones.
| shortDate | It converts the date to a short date. |
| longDate | It provides the long date for the date. Provides full month name then day and year. |
| fullDate | It provides the full date for the date. i.e day, month date, year |
Please find the list of date pipes here.
Code for the same,
HTML part
- <h2>Date pipes</h2>
- <h3>{{currentDate}}</h3>
- <h3>{{currentDate | date:'shortDate'}}</h3>
- <h3>{{currentDate | date:'longDate'}}</h3>
- <h3>{{currentDate | date:'fullDate'}}</h3>
TypeScript part
this.currentDate = new Date();
output

Other pipes
Let’s talk about some more handy pipes available in Angular5.
| Currency | Formats a number as currency. |
| Currency : <currency type of country> | Formats a number as a currency of the specific country. It takes country currency type as a parameter. |
| DeprecatedCurrencyPipe | Formats a number as currency to a specific decimal format. |
Code for the same,
- <td>{{credit.salary | currency}}</td>
- <td>{{credit.salary | currency : 'INR'}}</td>
- <td>{{credit.salary | currency : 'INR' : true : '8.2'}}</td>
Output

Explanation
As you can observe the 1st salary column in USD.
As I pass the currency type (INR) as a parameter for the 2nd column, the output type is formatted to INR.
In the 3rd scenario, I wanted the data in a specific way i.e. 8 characters before decimal and 2 characters after the decimal. In the output 2, 0s are appended before the number.
Custom pipes in Angular 5
There are times in development you will need to create a pipe of your own to accommodate your specific requirement. For that Angular provides custom Pipe option. It’s really very easy to generate a custom Pipe using @Angular/Cli.
Following is the command you can use in the terminal(in Visual studio code) to generate your custom Pipe.

Sagar Pandurang KapPosted Feb 13, 2018, 11:00 PM
Nice article.What is the main use of pipes?