简体   繁体   English

如何使用 IoNIC 从国际电话号码获取电话本地号码

[英]How to get the phone local number from international phone number with IoNIC

I'm using IONIC4 in my project, I managed to get all the countries with its own CallingCodes and country code, Now I'm getting stuck to get the local phone format(a mask to display in the placeholder) for the selected country.我在我的项目中使用 IONIC4,我设法让所有国家/地区都有自己的 CallingCodes 和国家/地区代码,现在我陷入困境,无法获取所选国家/地区的本地电话格式(在占位符中显示的掩码)。

I give an example, suppose that the selected country is France, its country code is: FR and its calling code is (33) so how I get the following mask:我举个例子,假设选择的国家是法国,它的国家代码是:FR,它的调用代码是(33)所以我如何得到以下掩码:

07 45 85 96 32

Is there a way to do that using IONIC please ?请问有没有办法使用IONIC来做到这一点?

You could install libphonenumber: npm install --save-prod google-libphonenumber你可以安装 libphonenumber: npm install --save-prod google-libphonenumber

Then, you can use the AsYouTypeFormatter to mask the number as it's being typed.然后,您可以使用 AsYouTypeFormatter 来屏蔽正在键入的数字。

// Require `AsYouTypeFormatter`.
const AsYouTypeFormatter = require('google-libphonenumber').AsYouTypeFormatter;
const formatter = new AsYouTypeFormatter('US');

console.log(formatter.inputDigit('2')); // => 2
console.log(formatter.inputDigit('0')); // => 20
console.log(formatter.inputDigit('2')); // => 202
console.log(formatter.inputDigit('-')); // => 202-
console.log(formatter.inputDigit('4')); // => 202-4
console.log(formatter.inputDigit('5')); // => 202-45
console.log(formatter.inputDigit('6')); // => 202-456
console.log(formatter.inputDigit('-')); // => 202-456-
console.log(formatter.inputDigit('1')); // => 202-456-1
console.log(formatter.inputDigit('4')); // => 202-456-14
console.log(formatter.inputDigit('1')); // => 202-456-141
console.log(formatter.inputDigit('4')); // => 202-456-1414

// Cleanup all input digits from instance.
formatter.clear();

To get the format in France, simply pass 'FR' to the formatter要在法国获得格式,只需将“FR”传递给格式化程序

const formatter = new AsYouTypeFormatter('FR');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM