编辑(15.03.2018)-感谢Joseph Webber
首先,您必须安装
libphonenumber-js
,它是google libphonenumber的包装器,准备在Angular 2+上导入。
npm install libphonenumber-js --save
或
yarn add libphonenumber-js
取决于您使用的包管理器。
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');
}
}
我在Github上添加了工作演示:
libphonenumber-demo-angular2