【问题标题】:getting directive scope values as undefined with attribute directive in angularjs使用angularjs中的属性指令获取指令范围值未定义
【发布时间】:2019-04-02 10:26:31
【问题描述】:

在 angularjs 指令中声明的范围变量未定义。 下面的代码有什么问题?

<input type="text" class="form-control" id="amount"
 name="amount" ng-model="amount" required ng-pattern="Pattern"
 min="25" max="{{availableAmount}}" input-range-check/>

脚本

app.directive('inputRangeCheck', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            min: '@',
            max: '@'
        },
        link: function (scope, element, attributes, ngModel) {          
            ngModel.$validators.minError = function (modelValue) {
                console.log("--------------------------",min)
                return modelValue < min;
            };
            ngModel.$validators.maxError = function (modelValue) {
                return modelValue > max;
            };          
            scope.$watch(attributes.ngModel, function(value) {
                ngModel.$validate();
            });
        }
    };
});

在链接函数中得到未定义的最小值和最大值?

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

    范围对象中提供了最小值和最大值。

    这个功能会让你明白。

      link: function (scope, element, attributes, ngModel) {          
            ngModel.$validators.minError = function (modelValue) {
                console.log("--------------------------",scope.min)
                return modelValue < scope.min;
            };
            ngModel.$validators.maxError = function (modelValue) {
                return modelValue > scope.max;
            };          
            scope.$watch(attributes.ngModel, function(value) {
                ngModel.$validate();
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      相关资源
      最近更新 更多