Angular Pipe for Phone Number Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular 11/10 Create Custom Pipe Example
- Angular Pipes Example | Angular Pipes List Tutorial
- Angular KeyValue Pipe Example | KeyValue Pipe in Angular
- Angular Titlecase Pipe Example | Titlecase Pipe in Angular
- Angular Slice Pipe Example | Slice Pipe in Angular
- Angular Decimal Pipe Example | Number Pipe in Angular
- Angular Date Pipe Example | Date Pipe in Angular
- Angular Currency Pipe Example | Currency Pipe in Angular
- How to Use Angular Pipe in Component Class?
- How to Pass Multiple Parameters to Pipe in Angular?
- Angular Nl2br Pipe Example
- How to Create Custom Pipe in Angular 9/8?