Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sample for @angular-libphonenumber with angular 2 or higher?

I am trying to create a directive for formatting and validating the phone numbers in my angualr 4 application, was looking for some guidance to getting started.

like image 354
ram_p Avatar asked Jul 10 '17 19:07

ram_p


1 Answers

Edited (15.03.2018) - thanks @Joseph Webber

First, you have to install libphonenumber-js, which is a wrapper of google-libphonenumber ready to be imported on Angular 2+. You can install it on your app with:

npm install libphonenumber-js --save

or

yarn add libphonenumber-js

depending on the package manager you use.

After install you can use it on your component like:

import { Component, OnInit } from '@angular/core';
import { parse, format, AsYouType } from 'libphonenumber-js';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  asYouType: any;
  format: any;
  parse: any;

  ngOnInit() {
    this.asYouType = new AsYouType('US').input('2133734');
    this.format = format('2133734253', 'US', 'International');
    this.parse = parse('(0777) 844 822', 'RO');
  }

}

I added the working demo on Github:

libphonenumber-demo-angular2

like image 96
BogdanC Avatar answered Sep 28 '22 07:09

BogdanC