Angular 2: pipes - How to format a phone number?

前端 未结 5 1654
灰色年华
灰色年华 2020-12-14 17:18

I\'ve searched here and there, and I am not able to find something specific about formatting a phone number.

Currently, I am retrieving phone numbers form a JSON in

5条回答
  •  庸人自扰
    2020-12-14 17:38

    You may use something like that in a custom Angular2 pipe:

    switch (value.length) {
            case 10: 
                country = 1;
                city = value.slice(0, 3);
                number = value.slice(3);
                break;
    
            case 11: 
                country = value[0];
                city = value.slice(1, 4);
                number = value.slice(4);
                break;
    
            case 12: 
                country = value.slice(0, 3);
                city = value.slice(3, 5);
                number = value.slice(5);
                break;
    
            default:
                return tel;
        }
    

    Check this AngularJS out for more info, but as I said you need to convert it to Angular2:

    http://jsfiddle.net/jorgecas99/S7aSj/

提交回复
热议问题