如何在angularjs中做条件maxlength

时间:2014-07-24 11:32:30

标签: angularjs validation angularjs-scope

有没有人知道如何在angularjs中做条件maxlength,我找到了条件要求但不是maxlength等....

例如,如果maxlength给定...它应该验证但是如果maxlength是0或false,它不应该....

$scope.validation = {a: {required:true,minlength:5, maxlength:10}, 
                     b: {required:false,minlength:0, maxlength:false}}

使用ng-repeat循环创建Html ....现在问题是每次都会激活maxlength ...而我已经为第二个输入字段禁用了它

<input type="text" name="myName" ng-model="name" ng-minlength='validation.a.minlength' ng-maxlength="validation.a.maxlength" ng-required='validation.a.required'  />


<input type="text" name="myName1" ng-model="name1" ng-minlength='validation.b.minlength' ng-maxlength="validation.b.maxlength" ng-required='validation.b.required' />

1 个答案:

答案 0 :(得分:2)

如果您通过角度表达式将空字符串用作“false”,则这些参数似乎有效。

<input type="text" name="myName" ng-model="name" ng-minlength='{{validation.a.minlength}}' ng-maxlength="{{validation.a.maxlength}}" ng-required='validation.a.required'  />
<input type="text" name="myName1" ng-model="name1" ng-minlength='{{validation.b.minlength}}' ng-maxlength="{{validation.b.maxlength}}" ng-required='validation.b.required' />

  $scope.validation = {
    a: {required:true, minlength:5, maxlength:10}, 
    b: {required:false, minlength:'', maxlength:''}
  }

<击> plunker

plunker for 1.3.2

现在可以使用0和Number.MAX_SAFE_INTEGER来忽略此规则。或者只有999就足够了:))

 b: {required:false, minlength:0, maxlength:Number.MAX_SAFE_INTEGER}