对于角度为2或更高的@ angular-libphonenumber,是否有任何样本?

ram*_*m_p 10 libphonenumber

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

Bog*_*anC 11

编辑(15.03.2018) - 谢谢@Joseph Webber

首先,你必须安装libphonenumber-js,它是google-libphonenumber的包装器,可以在Angular 2+上导入.您可以将其安装在您的应用上:

npm install libphonenumber-js --save
Run Code Online (Sandbox Code Playgroud)

要么

yarn add libphonenumber-js
Run Code Online (Sandbox Code Playgroud)

取决于您使用的包管理器.

安装后,您可以在组件上使用它,如:

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');
  }

}
Run Code Online (Sandbox Code Playgroud)

我在Github上添加了工作演示:

libphonenumber-演示angular2