Angular Parameterized Pipes

Angular Parameterized Pipes with Examples

In this article, I am going to discuss Angular Parameterized Pipes in detail. Please read our previous article where we discussed the basics of Angular Pipes. At the end of this article, you will understand what exactly Angular Parameterized Pipes are and when and how to use these pipes in Angular Application?

What are Angular Parameterized Pipes?

In Angular, we can pass any number of parameters to the pipe using a colon (:) and when we do so, it is called Angular Parameterized Pipes. The syntax to use Parameterized Pipes in Angular Application is given below.

What are Angular Parameterized Pipes?

Date Pipe:

Let us understand the Parameterized Date pipes with some examples. When you worked with any real-time applications, then you need to display the date time data in different formats. Here, I am going to show you some of the formats then I will provide you the link from where you will get all the available data formats.

Step1: Modify app.modules.ts file

Open app.module.ts file and then copy and paste the following code in it. Here we setting AppComponent as our startup file.

importĀ {Ā BrowserModuleĀ }Ā fromĀ '@angular/platform-browser';
importĀ {Ā NgModuleĀ }Ā fromĀ '@angular/core';
importĀ {Ā FormsModuleĀ }Ā fromĀ '@angular/forms';

importĀ {Ā AppRoutingModuleĀ }Ā fromĀ './app-routing.module';
importĀ {Ā AppComponentĀ }Ā fromĀ './app.component';

@NgModule({
Ā Ā declarations:Ā [
Ā Ā Ā Ā AppComponent,
Ā Ā ],
Ā Ā imports:Ā [
Ā Ā Ā Ā BrowserModule,
Ā Ā Ā Ā AppRoutingModule,
Ā Ā Ā Ā FormsModuleĀ 
Ā Ā ],
Ā Ā providers:Ā [],
Ā Ā bootstrap:Ā [AppComponent]
})
exportĀ classĀ AppModuleĀ {Ā }
Step2: Modify app.component.ts file

Open app.component.ts file and then copy and paste the following code in it. Here we simply create a variable i.e. today to hold the current data. As you can see you can use Date.now() to get the current date in typescript.

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

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

Open app.component.html file and then copy and paste the following code in it. As you can see, here we are using the parameterized data pipe to show different date formats.

<p>DateĀ PipeĀ :Ā {{todayĀ |Ā date}}</p>
<p>FullĀ DateĀ :Ā {{todayĀ |Ā date:'fullDate'}}</p>
<p>MediateĀ DateĀ :Ā {{todayĀ |Ā date:'medium'}}</p>
<p>ShortĀ DateĀ :Ā {{todayĀ |Ā date:'short'}}</p>
<p>DateĀ (dd/MM/yyyy)Ā :Ā {{todayĀ |Ā date:'dd/MM/yyyy'}}</p>
<p>TimeĀ :Ā {{todayĀ |Ā date:'h:mmĀ aĀ z'}}</p>
<p>MediumĀ TimeĀ :Ā {{todayĀ |Ā date:'mediumTime'}}</p>
Step4: Modify index.html file

Finally open index.html file and then copy and paste the following code in it.

<!doctypeĀ html>
<htmlĀ lang="en">
<head>
Ā Ā <metaĀ charset="utf-8">
Ā Ā <title>MyAngularApp</title>
Ā Ā <baseĀ href="/">
Ā Ā <metaĀ name="viewport"Ā content="width=device-width,Ā initial-scale=1">
Ā Ā <linkĀ rel="icon"Ā type="image/x-icon"Ā href="favicon.ico">
</head>
<body>
Ā Ā <app-root></app-root>
</body>
</html>

With the above changes in place nor run the application and you should the date in different formats in the web page as shown in the below image.

Data Pipes Examples in Angular Application

Please visit the following link for the complete list of angular date pipes.

https://angular.io/api/common/DatePipe

Currency Pipe:

The Angular Currency Pipe is used to transforms a number to a currency string, formatted according to locale rules that determine group sizing and separator, decimal-point character, and other locale-specific configurations. Let us understand this with an example.

Step1: Modify app.component.ts

Open app.component.ts file and then copy and paste the following code in it. Here, we just created one property of type number.

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

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

Open app.component.html file and then copy and paste the following code in it.

<p>CurrencyĀ USDĀ inĀ SymbolĀ :Ā {{salaryĀ |Ā currency:'USD':true}}</p>
<p>CurrencyĀ INRĀ inĀ SymbolĀ :Ā {{salaryĀ |Ā currency:'INR':true}}</p>

<p>CurrencyĀ USDĀ inĀ CodeĀ :Ā {{salaryĀ |Ā currency:'USD':false:'4.2-2'}}</p>
<p>CurrencyĀ INRĀ inĀ CodeĀ :Ā {{salaryĀ |Ā currency:'INR':false:'1.3-3'}}</p>
Let us understand the above code.
  1. The first parameter is the currency Code (i.e. USD or INR)
  2. The second parameter is boolean – True to display the currency symbol where as false to display the currency code.
  3. The third parameter (ā€˜1.3-3’ or ā€˜4.2-2’) specifies the number of integer and fractional digits.

Now save the changes and have a look at the browser and you should get the following output.

Currency Pipes Example in Angular Application

In the next article, I am going to discuss Creating Angular Custom Pipes with examples. Here, in this article, I try to explain Angular Parameterized Pipes in detail. I hope you enjoy this article and understand the concept of Parameterized Pipes.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

Leave a Reply

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