angular限制输入框整数和小数的指令

(function () {
    'use strict';
    angular.module('common')
        .directive('numFormat', function () {
            return {
                restrict: 'A',
                require: 'ngModel',
                scope: {
                    isInt: '@',
                    decimal: '@',
                },
                controller: ['$scope', function ($scope) {
                }],
                link: function (scope, $element, $attr, ngModelCtrl) {
                    function format() {
                        if (ngModelCtrl.$modelValue === null) {
                            ngModelCtrl.$setViewValue(null);
                            ngModelCtrl.$render();
                        } else if ( isNaN(ngModelCtrl.$modelValue) || ngModelCtrl.$modelValue === undefined) {
                            ngModelCtrl.$setViewValue(null);
                            $element.val(null);
                            ngModelCtrl.$render();
                        } else {
                            if (scope.isInt === 'true') {
                                var f = Math.round(ngModelCtrl.$modelValue * 100) / 100;
                                var s = f.toString();
                                ngModelCtrl.$setViewValue(parseInt(s));
                                ngModelCtrl.$render();
                            } else if (scope.decimal) {
                                // 保留任意位小数
                                var s = ngModelCtrl.$modelValue.toString();
                                // var s = viewValue.length > modelValue.length ? viewValue:modelValue;
                                var rs = s.split('.');
                                if (rs.length > 1) {
                                    if (rs[1].length > parseInt(scope.decimal)) {
                                        rs[1] = rs[1].slice(0, scope.decimal)
                                        ngModelCtrl.$setViewValue(parseFloat(rs.join('.')));
                                    }

                                    var viewValue = ngModelCtrl.$viewValue.toString();
                                    viewValue = viewValue.split('.')
                                    if (viewValue[1].length > parseInt(scope.decimal)) {
                                        viewValue[1] = viewValue[1].slice(0, scope.decimal)
                                        ngModelCtrl.$setViewValue(parseFloat(viewValue.join('.')));
                                    }
                                }
                                ngModelCtrl.$render();
                            } else {
                                // 最多保留两位小数
                                var f = Math.round(ngModelCtrl.$modelValue * 100) / 100;
                                console.log('numform:',f)
                                var s = f.toString();
                                var rs = s.indexOf('.');
                                if (rs >= 0) {
                                    console.log('numform1:',s)
                                    ngModelCtrl.$setViewValue(parseFloat(s));
                                    ngModelCtrl.$render();
                                } else {
                                    // ngModelCtrl.$setViewValue(parseInt(s));
                                }
                            }
                        }
                    }

                    $element.keyup(format)
                }
            }
        })
})();

使用:

                <input min="0"  step="0.01" type="number" num-format class="form-control"  ng-model="money">

猜你喜欢

转载自www.cnblogs.com/kaibo520/p/10410518.html
今日推荐