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

Is there any sample for @angular-libphonenumber with angular 2 or higher?

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

已编辑 (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