Format input value in Angularjs

后端 未结 3 465
长发绾君心
长发绾君心 2020-11-30 23:38

I\'m trying to write a directive that automatically formats a number in the but the the model isn\'t formatted. Getting it to work is fine, on lo

3条回答
  •  生来不讨喜
    2020-12-01 00:13

    Small edit to Maxim Shoustin's answer below as per the answer to this question: AngularJS formatter - how to display blank instead of zero

    Only change is to ensure that the input is blank rather than zero on the deletion of the last number:

       ctrl.$parsers.unshift(function (viewValue) {
            console.log(viewValue);
            if(viewValue){
                var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
                elem.val($filter('number')(plainNumber));
                return plainNumber;
            }else{
                return '';
            }
        });
    

    http://jsfiddle.net/2n73j6rb/

提交回复
热议问题