ItSolutionStuff.com

Angular Pipe for Phone Number Example

By Hardik Savani • May 2, 2024
Angular

Hi,

This is a short guide on angular pipe for phone number. We will use angular phone number pipe. i explained simply about phone number pipe angular. This tutorial will give you simple example of phone number format pipe angular.

you can easily create custom pipe for phone number format in angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 version.

here, you have to follow few step to create simple example of custom pipe for phone number format. let's see one by one.

Step 1: Create New App

If you are doing example from scratch then You can easily create your angular app using bellow command:

ng new app-material

Step 2: Create Custom Pipe

We need to run following command to creating pipe in angular application.

ng generate pipe phone-format

Now we need to write some logic on our custom pipe ts file. so let's write logic as i written for demo now.

app/phone-format.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({

name: 'phoneFormat'

})

export class PhoneFormatPipe implements PipeTransform {

transform(number) {

number = number.charAt(0) != 0 ? "0" + number : "" + number;

let newStr = "";

let i = 0;

for (; i < Math.floor(number.length / 2) - 1; i++) {

newStr = newStr + number.substr(i * 2, 2) + "-";

}

return newStr + number.substr(i * 2);

}

}

now it should be import in module.ts file as bellow:

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 { PhoneFormatPipe } from './phone-format.pipe';

@NgModule({

imports: [ BrowserModule, FormsModule ],

declarations: [ AppComponent, PhoneFormatPipe ],

bootstrap: [ AppComponent ]

})

export class AppModule { }

Step 3: Use Custom Pipe

Now we need to create one variables and use it, let's add code as like bellow:

app/app.component.ts

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

@Component({

selector: 'my-app',

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

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

})

export class AppComponent {

name = 'Angular ' + VERSION.major;

phoneNumber = "25565220";

}

Ok, now we can use 'nullWithDefault' custom pipe in html file, so let's write it.

app/app.component.html

<p>Phone: {{ phoneNumber | phoneFormat }}</p>

Output:

Phone: 02-55-65-220

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 11/10 Create Custom Pipe Example

Read Now →

Angular Pipes Example | Angular Pipes List Tutorial

Read Now →

Angular KeyValue Pipe Example | KeyValue Pipe in Angular

Read Now →

Angular Titlecase Pipe Example | Titlecase Pipe in Angular

Read Now →

Angular Slice Pipe Example | Slice Pipe in Angular

Read Now →

Angular Decimal Pipe Example | Number Pipe in Angular

Read Now →

Angular Date Pipe Example | Date Pipe in Angular

Read Now →

Angular Currency Pipe Example | Currency Pipe in Angular

Read Now →

How to Use Angular Pipe in Component Class?

Read Now →

How to Pass Multiple Parameters to Pipe in Angular?

Read Now →

Angular Nl2br Pipe Example

Read Now →

How to Create Custom Pipe in Angular 9/8?

Read Now →