我正在尝试创建一个用于格式化和验证我的angualr 4应用程序中的电话号码的指令,正在寻找一些入门指南。
发布于 2017-07-11 05:03:49
编辑(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上添加了工作演示:
https://stackoverflow.com/questions/45019823
复制相似问题