In Angular application, sometimes, we needs to show formatted data to the users, instead of raw data. Like date data, it would be great, if we can show date like December 10, 2018 instead of 12/10/2018 or 12-18-2018.
In earlier version of Angular <2.0, pipes were knows as Filters. In later Angular version >2+, these filters are known as Pipes to format the data.
To read more about Filters in AngularJS (<v2), click the below link:
How to create and use filters in AngularJS
The pipe (|) symbol is used for formatting the data. There are some built-in pipes in Angular. These are mentioned here.
Angular Built-In Pipes
- Uppercasepipe
- Lowercasepipe
- Currencypipe
- Datepipe
- Jsonpipe
- Decimalpipe
- Percentpipe
- Slicepipe
Let’s go through details of these pipes one by one.
Uppercasepipe
|
|
|
|
The following line of code will format the title
in uppercase
.
Lowercasepipe
|
|
The following line of code will format the title
in lowercase
.
Currencypipe
|
|
|
|
Here, in our app.component.ts
file, we have amount
property, and we are format this in USD currency
using these two lines of codes.
Datepipe
|
|
|
|
Here, we showed current date in different format using the parameters.
Jsonpipe
|
|
|
|
Here, we have created a json object addressJson
, and apply the Json
pipe to render on the browser.
Decimalpipe
Decimalpipe
is used to format a number as a decimal number as per locale use. The syntax for writing this is:
Let’s see each part in detail:
number_expression
: this is an angular expression that will give output a number.
number
: number is a pipe keyword that will be used with pipe operator.
digitInfo
: defines number format.
Let’s dive into the details of digitInfo
syntax. The syntax is below:
Below is the description of each part.
minIntegarDigits
: minimum number of integer digits. Default is 1.
minFractionDigits
: minimum number of fraction digits. Default is 0.
maxFractionDigits
: maximum number of fraction digits. Default is 3.
Let’s have a look at few examples.
1. Using Default Format
{1}.{0}-{3}
Where
{1}: minIntegerDigit, {0}: minFractionDigit, {3}: maxFractionDigit
Let’s declare a property num
, and assign this value.
& in template file.
Output will be:
2. Using Format 3.2-5
{3}: minIntegerDigit, {2}: minFractionDigit, {5}: maxFractionDigit
In template file
Output will be:
3. Using Format 3.2-5
Declare another property num1, and assign the value.
In template file
Output will be:
|
|
|
|
Percentpipe
Percentpipe
format a number as a percent as per locale rules. The syntax for writing this is:
Let’s see each part in detail:
number_expression
: this is an angular expression that will give output a number.
percent
: percent is a pipe keyword that will be used with pipe operator and convert number into percent.
digitInfo
: defines percent format.
Let’s dive into the details of digitInfo
syntax. The syntax is below:
Below is the description of each part.
minIntegarDigits
: minimum number of integer digits. Default is 1.
minFractionDigits
: minimum number of fraction digits. Default is 0.
maxFractionDigits
: maximum number of fraction digits. Default is 3.
Let’s have a look at few examples.
1. Using Default Format
{1}.{0}-{3}
Where
{1}: minIntegerDigit, {0}: minFractionDigit, {3}: maxFractionDigit
Let’s declare a property numPercent
, and assign this value.
In template file
Output will be:
2. Using Format 2.2-5
{2}: minIntegerDigit, {2}: minFractionDigit, {5}: maxFractionDigit
In template file
Output will be:
|
|
|
|
SlicePipe
Slice
pipe is used to slice some part of an array or string. The syntax of SlicePipe
is below:
Let’s see details of above syntax.
string_or_array
: any string or array where we will apply Slice pipe.
slice
: a slice pipe
start
: the starting index of the subset to return
end
: the ending index of the subset to return
Let’s have a look at few examples. Create an number array numArr in component class.
Let’s slice this array with different parameters.
Output will be:
Another example of slice.
Output will be:
|
|
|
|
Summary
So, in this blog, we learned about different types of pipes used in Angular. We learned about their syntax, how to use them with different code snippets. Using pipes in angular, we can format data in a user friendly manner.
Reference
https://angular.io/guide/pipes
Further Reading
Share data between components in Angular
Fresh releases of Visual Studio 2017 for Mac and TypeScript 3.2