是否有角度为 2 或更高的 @angular-libphonenumber 的样本?



我正在尝试创建一个指令来格式化和验证我的angualr 4应用程序中的电话号码,正在寻找一些入门指南。

> 已编辑 (15.03.2018( - 感谢@Joseph韦伯

首先,你必须安装 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

最新更新