In this tutorial we’ll see how to use Angular pipe in Component classes and Service classes in Angular.
Steps to use angular pipes in component classes (or Service)
If you want to use the pipe in more than one component class then configure it in app.module.ts class-
- Import the required Angular pipe and add it to the providers array.
- In the Component class inject the pipe and use it.
You can do these steps in Component class itself if specific Angular pipe is used in that class only.
Using Angular pipes in component class example
We’ll create a simple reactive form with fields to enter date and amount. Date has to be transformed using the DatePipe and amount using the CurrencyPipe in Component. For DatePipe configuration is done in App module where as for CurrencyPipe it is done in the Component class itself.
App Module (app.module.ts)
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { AppRoutingModule } from './app-routing.module'; import { ReactiveFormsModule } from '@angular/forms'; import { DatePipe } from '@angular/common'; @NgModule({ declarations: [ AppComponent, ], imports: [ BrowserModule, ReactiveFormsModule, AppRoutingModule ], providers: [DatePipe], bootstrap: [AppComponent] }) export class AppModule { }
App Component (app.component.ts)
import { CurrencyPipe, DatePipe } from '@angular/common'; import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; @Component({ selector: 'app-root', templateUrl: './app.component.html', providers: [CurrencyPipe] }) export class AppComponent implements OnInit{ membershipForm : FormGroup; constructor(private datePipe: DatePipe, private currencyPipe: CurrencyPipe){ } ngOnInit() { this.membershipForm = new FormGroup({ mdate: new FormControl(this.datePipe.transform(Date.now(), 'yyyy-MM-dd')), amount: new FormControl(null) }); } onSubmit(){ console.log("Date-" + this.membershipForm.value.mdate); console.log("Currency-" + this.currencyPipe.transform(this.membershipForm.value.amount, 'USD')); } }
In the Component class you can notice that-
- Instance of both DatePipe and CurrencyPipe is created in the Constructor which is injected by the Angular.
- For CurrencyPipe configuration is done in the Component class itself by adding it to the providers array with in the component.
- A reactive form named membershipForm is created with two fields ‘mdate’ and ‘amount’. Date field is pre-filled with the current date. Date is transformed to have the value in 'yyyy-MM-dd' format. Angular 6 onward you can also use formatDate(Date.now(), 'yyyy-MM-dd', ,this.locale). You will have to inject the required Locale if using formatDate() method.
- In the onSubmit() values of mdate and amount fields are logged to console which should be trasnformed as per the given format.
- Amount is transformed to have $ as currency.
Template (app.component.html)
<div class="container"> <div class="row"> <div class="col-xs-12 col-sm-10 col-md-8"> <h1>Membership Form</h1> <form [formGroup]="membershipForm" (ngSubmit)="onSubmit()"> <div class="form-group"> <label for="mdate">Date</label> <input type="date" class="form-control" id="mdate" formControlName="mdate"> </div> <div class="form-group"> <label for="amount">Amount</label> <input type="number" class="form-control" id="amount" formControlName="amount"> </div> <button type="submit" class="btn btn-success">Submit</button> </form> </div> </div> </div>
That's all for this topic Using Angular Pipes in Component or Service Classes. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Angular Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment