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:
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
标签:
libphonenumber