问题描述
我正在尝试创建一个指令来格式化和验证angualr 4应用程序中的电话号码,正在寻找入门指南.
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.
推荐答案
已编辑(15.03.2018)-感谢@Joseph Webber
Edited (15.03.2018) - thanks @Joseph Webber
首先,您必须安装 libphonenumber-js ,它是google的包装器-libphonenumber准备在Angular 2+上导入.您可以使用以下方法将其安装在您的应用上:
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
或
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');
}
}
我在Github上添加了工作示例:
I added the working demo on Github:
这篇关于@ angular-libphonenumber是否有角度为2或更高的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!