代码之家  ›  专栏  ›  技术社区  ›  ram_p

有没有角度为2或更高的@angular libphonenumber的样本?

  •  12
  • ram_p  · 技术社区  · 7 年前

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

    1 回复  |  直到 7 年前
        1
  •  14
  •   BogdanC    6 年前

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