自定义指令的数量和类型的限制

时间:2016-07-26 11:16:29

标签: javascript angularjs angularjs-directive

我使用在线提供的自定义指令,它工作得非常好,但我只需要进行一项更改,例如在指令中可以限制十进制数或int,我可以使用逗号或点,唯一的问题是当我从服务器加载数据时,在数据库中它带有一个点,我喜欢当它第一次从db加载时它带有一个逗号而不是一个点。我尝试使用“replace('。',',')替换上面的$ parsers函数,但总是更改它,而不是让用户改为放点或点或逗号。 所以基本上我需要从db首次加载时,它带有一个逗号。

指令:

angular.module('myApp')
    .directive('onlyNumber', function() {

        return {
            require: 'ngModel',
            link: function ($scope, elem, attrs, ngModel) {
                var decRegexp, intRegexp;

                intRegexp = /^(\d*)/;
                decRegexp = "^(\\d*(\\.|,)?(\\d{1,DECIMALS})?)";
                decRegexp = new RegExp(decRegexp.replace('DECIMALS', ''+attrs.decimalUpto));

  // I tried this above but isnt updating the input/view
 var getter = $parse(attrs.ngModel);
            var value = getter($scope);
            if(value){
            ngModel.$setViewValue(value.replace('.', ','));
            ngModel.$render();
            }


                ngModel.$parsers.push(function (val) {


                    var isDec, parsed, ref, regexp;
                    isDec = attrs.numType === 'decimal';

                    regexp = isDec ? decRegexp : intRegexp;
                    parsed = val != null ? (ref = val.match(regexp)) != null ? ref[0] : void 0 : void 0;
                    ngModel.$setViewValue(parsed);
                    ngModel.$render();

                    if(isDec){

                        var result = parseFloat(parsed.replace(',', '.'));
                        if (attrs.minLimit > result) {
                            ngModel.$setValidity('smartFloatMin', false);
                            return undefined;
                        }else
                            ngModel.$setValidity('smartFloatMin', true);



                        if (attrs.maxLimit < result) {
                            ngModel.$setValidity('smartFloatMax', false);
                            return undefined;
                        }else
                            ngModel.$setValidity('smartFloatMax', true);
                    }else{
                        var result = parseInt(parsed);

                        if (attrs.minLimit > result) {
                            ngModel.$setValidity('smartIntegerMin', false);
                            return undefined;
                        }else
                            ngModel.$setValidity('smartIntegerMin', true);



                        if (attrs.maxLimit < result) {
                            ngModel.$setValidity('smartIntegerMax', false);
                            return undefined;
                        }else
                            ngModel.$setValidity('smartIntegerMax', true);
                    }


                    return result;
                });

            }
        };
    });

1 个答案:

答案 0 :(得分:0)

在你的指令中,像这样使用它

  ngModelCtrl.$formatters.push(function(modelValue) {
    return modelValue.replace(/\./g,',') ;
  })

here is a working plunker