【问题标题】:limitation of numbers and types of custom directive自定义指令的数量和类型的限制
【发布时间】:2016-07-26 11:16:28
【问题描述】:

我正在使用在线提供的自定义指令,它工作得非常好,但我只需要进行一项更改,例如在指令中我能够限制十进制数或整数,我可以使用逗号或点,唯一的问题是当我从服务器加载数据时,在数据库中它带有一个点,我喜欢第一次从数据库加载时它带有一个逗号而不是一个点。我尝试使用“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;
                });

            }
        };
    });

【问题讨论】:

  • 尝试使用 $formaters.push() 而不是 $parsers.push()。 $parsers.push for viewVale 不是 modelValue!
  • 你能显示你的视图代码吗?

标签: javascript angularjs angularjs-directive


【解决方案1】:

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

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

here is a working plunker

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2020-02-11
    • 1970-01-01
    • 2019-01-04
    相关资源
    最近更新 更多