our

Latest Blogs

A Detailed Guide on Angular Parameterized Pipes

iFour Team -June 28, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
A Detailed Guide on Angular Parameterized Pipes

What are Pipes in Angular application?

The Angular Pipe takes the data as input and then transforms the raw data into the desired format. In other words, we can say that the angular pipes are used to transform the data into a specific format before displaying them to the end-users.

Pipes are one find of functions that you can use in template expressions to accept an input value and return a transformed value. We can declare the pipes once and can use them throughout the application.

We can use pipe features by using the pipe (|) operator to any property in the angular application. Angular Framework provides some built-in pipes such as lowercase, uppercase, titlecase, decimal, date, percent, currency, etc. It is also possible to create custom pipes according to our requirements.

Syntax -

Use the pipe operator (|) in the template expression to apply a pipe.

 

{{ input data | PipeName }}

 

Types of pipes in Angular

Pipes in angular framework are divided into two types i.e. Built-in Pipes and Custom pipes. And the Built-in Pipes are further divided into two types i.e. Parameterized and chaining.

Types of pipes

Figure 1 Types of pipes

What are parameterized Pipes in Angular?

In an angular application, when we pass any parameters to the pipes, it is called parameterized pipes we can pass n number of parameters to pipes using a colon (:).

Syntax of parameterized pipe

{{ input data | PipeName : parameter1 : parameter2 ... parameter n }}

We will be using parameterized date pipes and parameterized currency pipes in our application: In the date pipe, we can specify the date format by passing it as a parameter. In currency pipes, we can specify different currency codes as a parameter to transform them into a particular currency format.

Example 1

Create an angular application named “parameterized-pipes” by executing the following command:

 

ng new parameterized-pipes

 

For parameterized date pipes the format is as following:

 

{{ input data | date [ : format [ : timezone [ : locale ] ] ] }}

Parameters

 

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  today : number= Date.now();
}

In the typescript file, we create a variable “today” to store the current data. We use Date.now() method to fetch the current date and time.

app.component.html

Current Date : {{today | date}}
Full Date : {{today | date:fullDate}}
Medium Date : {{today | date:medium}}
Short Date : {{today | date:short}}
Date (dd/MM/yyyy) : {{today | date:dd/MM/yyyy}}
Time : {{today | date:h:mm a z}}
Medium Time : {{today | date:mediumTime}}

In the template file, we are using the parameterized data pipe to show different date formats.

We have used some pre-defined format options as a parameter such as fullDate, medium, short, mediumtime. And in some pipes, we have specified the format in which we want to convert the current date and time.

“date” is a simple non-parameterized pipe that is used to convert the current date in the MMM dd, yyyy format.

The "fullDate" parameter will specify the current date and day of the week in the format:EEEE,MMMM d,y.

The “medium” parameter will transform the date in the format MMM d, y, h:mm:ss a.

The “short” parameter will transform the date in the format M/d/yy, h:mm a.

The “mediumTime” will show the current time in the h:mm:ss a.

This code will generate the following output.

Parameterized date pipe

Figure 2-Parameterized date pipe

Example 2

Create an angular application named “parameterized-pipes” by executing the following command:

ng new parameterized-pipes

One-stop-oplossing voor Angular-webontwikkeling -Uw zoekopdracht eindigt hier.

Format of parameterized currency pipes:

{{ input data | currency [ : currencyCode [ : display [ : digitsInfo [ : locale ] ] ] ] }}

Parameters

The following code specifies how the pipe transforms currency into text strings, according to various format specifications.

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    salary: number = 456723.50;
}

In the typescript file, we have created a variable “salary” to store the data of salary.

app.component.html:

Currency : {{salary | currency}}
Currency CAD in Code : {{salary | currency:CAD:code}}
Currency CAD in Symbol : {{salary | currency:CAD:symbol}}
Currency CAD in Symbol : {{salary | currency:CAD:symbol-narrow}}
Currency USD in Symbol : {{salary | currency:USD:true}}
Currency INR in Symbol : {{salary | currency:INR:true}}
Currency USD in Code : {{salary | currency:USD:false:4.2-2}}
Currency INR in Code : {{salary | currency:INR:false:1.3-3}}

In the template file, we are using the parameterized currency pipes to show currency in different formats.

“currency” is a non-parameterized pipe used to transform the number in a specific format. By default, this will transform the currency into USD.

-We need to specify the currency code such as USD, INR, or CAD as the first parameter.

-In the second parameter, we need to specify whether we want to display the currency in symbol or code. For that, we can write ‘code’, ‘symbol’, ‘symbol-narrow’, or a Boolean value.

‘code is used to display the currency code. ‘symbol’ is used to display the currency symbol. ‘symbol-narrow’ is used to display the currency in short currency symbol. We can set the Boolean value to True to display the currency. This Boolean value is set to false to display the currency code.

-The third parameter (‘1.3-3’ or ‘4.2-2’) determines the number of integer and fractional digits to be displayed.

The above will generate the following output.

Parameterized currency pipes

Figure 3 Parameterized currency pipes

Conclusion

Pipes are simple functions designed to accept an input value, process, and return a transformed value as the output. Angular supports many built-in pipes. Angular Pipes transform the output. They do not change the data but change their format and how they appear to the user. You can also customize the pipes according to transform data according to your requirements.

Work with us

Well do everything we can to make our next best project!

Our Insights

Check out our most recent blogs

An in-depth guide on Angular Dependency Providers
An in-depth guide on Angular Dependency Providers

What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...

A simple guide on AOT Compilation in Angular
A simple guide on AOT Compilation in Angular

What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...

Implement Attribute, Class, and Style binding in Angular
Implement Attribute, Class, and Style binding in Angular

What is binding in Angular? Binding in angular apps is the automatic synchronization of data within the model and view components. You can use data binding to define things...

Our Partners

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Work With Us

Well do everything we can to make our next best project!