Working with Angular Pipes

Angular Pipes takes data as input and formats or transform the data to display in the template. We use them to change the appearance of the data before presenting it to the user. The most common use case of pipes is displaying the dates in the correct format as per the user’s locale.

In this tutorial, we will show you how to use Angular Pipes. We will see how to pass arguments to the pipe and how to chain multiple pipes. We are also going to look at the few of the angular built-in pipes like currency pipe, date pipe, number pipe, percent pipe, decimal pipe, & slice pipe etc.

Angular Pipes Syntax

The syntax of the pipe is as follows

Where

Expression: is the expression, which you want to transform
| : is the Pipe Character
pipeOperator : name of the Pipe
pipeArguments: arguments to the Pipe

Pipes Example

In this example let use Angular built in date pipe to transform the date

Component class

In the above example, we are taking current date and transforming it into the easily readable format using the date pipe. We have included the unformatted date format for comparison. The output is as shown below

Angular Pipes Example
Example of Angular Pipes

Passing arguments to pipes

We can also pass optional arguments to the pipe. The arguments are added to the pipe using a colon (:) sign followed by the value of the argument. If there are multiple arguments separate each of them with the colon (:). For example, we can pass the format as the argument to the date pipe, which is Optional. The medium is one of the valid value of the format argument, which displays the date in yMMMdjms format. The example code is as shown below.

The parameter medium displays the date as Nov 22, 2016, 10:04:10 PM

Chaining Pipes

Pipes can be chained together to make use of multiple pipes in one expression. For example in the following code, the toDate is passed to the Date Pipe. The output of the Date pipe is then passed to the uppercase pipe.

The Angular Built-in pipes

The Angular has several built-in pipes, which you can use in your application. You can read about them from this link

Some of the important pipes are Date Pipe, Uppercase Pipe, Lowercase Pipe, Number Pipe/ Decimal Pipe, Currency Pipe, and Percent Pipe, etc

DatePipe

The Date pipe formats the date according to locale rules. The syntax of the date pipe is as shown below

Where

date_expression is a date object or a number

date is the name of the pipe

format is the date and time format string which indicates the format in which date/time components are displayed.

Some of the common format strings are

ComponentformatExample
Yeary2016
Yearyy16
MonthM9
MonthM99
MonthMMMNov
MonthMMMMNovember
Dayd9
Daydd09
hourj9
hourjj09
hourh9 AM
hourhh09 AM
hour24H13
hour24HH13
minutem9
minutemm09
seconds9
secondss99
Time zonezPacific Standard time
Time zoneZGMT-8:00
Time zoneaPM
eraGAD
eraGGGGAnno Domini

Format argument also supports some predefined commonly used formats

Format NameEquivalent Format strngExample
(for en-US)
mediumyMMMdjmsSep 3, 2010, 12:05:08 PM
shortyMdjm9/3/2010, 12:05 PM
fullDateyMMMMEEEEdFriday, September 3, 2010
longDateyMMMMdSeptember 3, 2010
mediumDateyMMMdSep 3, 2010
shortDateyMd9/3/2010
mediumTimejms12:05:08 PM
shortTimejm12:05 PM

You can read about the complete list from link

Example of Datepipe

UpperCasePipe & LowerCasePipe

As the name suggests, these pipes transform the string to Uppercase or lowercase

Read more about uppercasepipe & lowercasepipe

SlicePipe

Creates a new List or String containing a subset (slice) of the string or array. This Pipe uses the JavaScript API Array.prototype.slice() and String.prototype.slice().

Syntax

Where

array_or_string_expression is the string to slice

slice is the name of the pipe

start is the start position/index from where the slicing will start

end is the ending index/position in the array/string

The slice pipes take two arguments. The first argument start is the starting index of the string/array. The second argument end is the ending index of the string/array. If the start or end index is negative then the index is counted from end of the string/array

Example

Both the above examples will display Angular. You can read more about slice from this link

DecimalPipe / NumberPipe

The Decimal Pipe is used to Format a number as Text. This pipe will format the number according to locale rules.

Syntax

Where

number_expression is the number you want to format

number is the name of the pipe

digitInfo is a string which has the following format

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

Where

minIntegerDigits is the minimum number of integer digits to use. Defaults to 1.

minFractionDigits is the minimum number of digits after fraction. Defaults to 0.

maxFractionDigits is the maximum number of digits after fraction. Defaults to 3.

Example

PercentePipe

Formats the given number as a percentage according to locale rules.

Where

number_expression is the number you want to format

percent is the name of the pipe

digitInfo is a string which has the following format. It is similar to used in decimal pipe

Example code

More about Percent pipe from the link

CurrencyPipe

Formats a number as currency using locale rules.

Where

number_expression currency to format a number as currency.

Currency is the name of the pipe

currencyCode is the ISO 4217 currency code, such as USD for the US dollar and EUR for the euro.

symbolDisplay is a boolean indicating whether to use the currency symbol or code. Use true to display symbol and false to use code

digitInfo is similar to the one used in decimal pipe

Example

Read more about the currencyPipe from the link

References

  1. Angular Pipes
  2. Built in Pipes

10 thoughts on “Working with Angular Pipes”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top