ItSolutionStuff.com

How to Use Angular Pipe in Component Class?

By Hardik Savani • May 2, 2024
Angular

Hi All,

In this short tutorial we will cover an how to use angular pipe in component. This article goes in detailed on angular use datepipe in component. you will learn angular use currency pipe in component. We will use angular pipe in component.

We will use angular pipe in component file and you can use in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 application.

I will give you two example as following bellow:

1) Angular Use DatePipe in Component

2) Angular Use Currency Pipe in Component

Example: Angular Use DatePipe in Component

In this example we will use datepipe in component file and change date formate. i will create date variable and store current date then i will use datepipe and change date format in 'm/d/Y'. So let's see bellow files:

src/app/app.module.ts

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

import { BrowserModule } from '@angular/platform-browser';

import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

import { DatePipe } from '@angular/common';

@NgModule({

imports: [ BrowserModule, FormsModule ],

declarations: [ AppComponent ],

bootstrap: [ AppComponent ],

providers: [ DatePipe ]

})

export class AppModule { }

src/app/app.component.ts

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

import { DatePipe } from '@angular/common';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

constructor(private datePipe: DatePipe) {

var date = Date.now();

var newDate = this.datePipe.transform(date, 'MM/dd/yyyy');

console.log(newDate);

}

}

Output:

04/05/2020

Example: Angular Use Currency Pipe in Component

In this example we will use currency in component file and add currency formate. i will create newCurrency variable and store currency store then i will use currency and change curreny format in usd. So let's see bellow files:

src/app/app.module.ts

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

import { BrowserModule } from '@angular/platform-browser';

import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

import { CurrencyPipe } from '@angular/common';

@NgModule({

imports: [ BrowserModule, FormsModule ],

declarations: [ AppComponent ],

bootstrap: [ AppComponent ],

providers: [ CurrencyPipe ]

})

export class AppModule { }

src/app/app.component.ts

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

import { CurrencyPipe } from '@angular/common';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

constructor(private currencyPipe: CurrencyPipe) {

var newCurrency = this.currencyPipe.transform(500, 'USD');

console.log(newCurrency);

}

}

Output:

$500.00

I hope it can help you...

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Angular Nl2br Pipe Example

Read Now →

Angular NgModel Example | NgModel Directive In Angular

Read Now →

Angular NgStyle Example | NgStyle Directive In Angular

Read Now →

Angular NgSwitch Example | NgSwitch Directive In Angular

Read Now →

Angular NgIf Else | Ng If Else in Angular Example

Read Now →

Angular Image Upload Example Tutorial

Read Now →

How to Create Reusable Components in Angular 10/9/8?

Read Now →

How to Create Custom Validators in Angular 9/8?

Read Now →

How to Create Custom Pipe in Angular 9/8?

Read Now →