Is there any sample for @angular-libphonenumber wi

2020-08-14 07:23发布

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.

1条回答
forever°为你锁心
2楼-- · 2020-08-14 08:05

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

查看更多
登录 后发表回答