Money formatting directive in Angular

前端 未结 3 1478
孤独总比滥情好
孤独总比滥情好 2020-12-29 11:09

I need a directive for filtering a field for currency, so a user just needs to type and the decimal is implied.

Needs:

  1. Format decimal field as user typ
相关标签:
3条回答
  • 2020-12-29 11:46

    .:: Updated Answer - July 14 ::.


    Check this simple directive:

    app.directive('price', [function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, ngModel) {
                attrs.$set('ngTrim', "false");
    
                var formatter = function(str, isNum) {
                    str = String( Number(str || 0) / (isNum?1:100) );
                    str = (str=='0'?'0.0':str).split('.');
                    str[1] = str[1] || '0';
                    return str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + '.' + (str[1].length==1?str[1]+'0':str[1]);
                }
                var updateView = function(val) {
                    scope.$applyAsync(function () {
                        ngModel.$setViewValue(val || '');
                        ngModel.$render();
                    });
                }
                var parseNumber = function(val) {
                    var modelString = formatter(ngModel.$modelValue, true);
                    var sign = {
                        pos: /[+]/.test(val),
                        neg: /[-]/.test(val)
                    }
                    sign.has = sign.pos || sign.neg;
                    sign.both = sign.pos && sign.neg;
    
                    if (!val || sign.has && val.length==1 || ngModel.$modelValue && Number(val)===0) {
                        var newVal = (!val || ngModel.$modelValue && Number()===0?'':val);
                        if (ngModel.$modelValue !== newVal)
                            updateView(newVal);
    
                        return '';
                    }
                    else {
                        var valString = String(val || '');
                        var newSign = (sign.both && ngModel.$modelValue>=0 || !sign.both && sign.neg?'-':'');
                        var newVal = valString.replace(/[^0-9]/g,'');
                        var viewVal = newSign + formatter(angular.copy(newVal));
    
                        if (modelString !== valString)
                            updateView(viewVal);
    
                        return (Number(newSign + newVal) / 100) || 0;
                    }
                }
                var formatNumber = function(val) {
                    if (val) {
                        var str = String(val).split('.');
                        str[1] = str[1] || '0';
                        val = str[0] + '.' + (str[1].length==1?str[1]+'0':str[1]);
                    }
                    return parseNumber(val);
                }
    
                ngModel.$parsers.push(parseNumber);
                ngModel.$formatters.push(formatNumber);
            }
        };
    }]);
    

    And use it like this:

    <input type="text" ng-model="number" price >
    

    See it live in this PLUNKER (July 14)

    0 讨论(0)
  • 2020-12-29 11:56

    i think this can full fill your requirement

    https://github.com/FCSAmerica/angular-fcsa-number
    

    you can restrict the input that allows only numbers with decimals by default angular input validations or by using char code.

    0 讨论(0)
  • 2020-12-29 12:01

    Angular Numeric

    Angular Numeric is a sophisticated directive that implements a complete numeric input field.

    Very simple to use - yet powerful.

    <input numeric min="-20" max="100" decimals="3" />
    

    There are checks on min and max values. When the value falls below the minumum the value is set to the minumum value. When the value exceeds the maxiumum, the value is set to the maximum.

    Formatting is done on the blur event; thousand separator and decimal are based on the current Angular locale.

    The number of decimals can be set.

    https://www.npmjs.com/package/angular-numeric-directive

    0 讨论(0)
提交回复
热议问题