小编han*_*sch的帖子

如何强制执行角度验证指令?

我已经为我的表单创建了一个验证指令.它基本上根据来自另一个字段的数据验证字段值.

它完美无缺:-)

我的问题是,如果在执行验证后其他字段发生更改,则验证将不再运行.

var myApp = angular.module('myApp', [])

.directive('validateInteger', function() {
  return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
      ctrl.$parsers.unshift(function(viewValue) {
          var int1val = scope.int1;
          scope.int2valid = (viewValue > int1val) ? "valid" : undefined;
          if (scope.int2valid == "valid") {
              ctrl.$setValidity('higher', true);
              return viewValue;
          } else {
              ctrl.$setValidity('higher', false);
              return undefined;
        }
      });
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

jsfiddle:http://jsfiddle.net/hanspc/vCFFQ/

validation angularjs

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

angularjs ×1

validation ×1