0

以下行的正确方法是什么?

ng-pattern="(true)? apply-pattern : dont-apply-pattern"

我的问题是,我在ng-pattern不需要时隐藏了该字段,但仍然不允许表单有效。

我尝试禁用该字段,但没有运气。

代码是:

<input type="text" ng-hide="param.paramType == 'java.lang.boolean'" 
       ng-pattern="param.paramValidatePattern" class="form-control input-sm" name="paramV" 
       ng-model="param.paramValue" ng-required="param.paramType != 'java.lang.boolean'" 
       ng-disabled="param.paramType == 'java.lang.boolean'" />
4

1 回答 1

0

看看这个特殊条件是否适用于您的版本...

<form name='myFormName'ng-submit="submitFormFunction()">
<input type="text" name="mySpecialPatternField" .../>

在 JS 代码中(指令 || 控制器)

$scope.submitFormFunction = function(){
   if( specialConditionIsMet ){
       // then we don't want the pattern input to matter in form validation                                    
       $scope.myFormName.mySpecialPatternField.$setValidity('pattern', true); 
   }
}

可能需要是假的,我不记得了,但是它们在 angularjs 中与您认为它们应该是交换的。

如果您使用 $setValidity,并且您使用了并且对该输入进行了多个验证(除了模式,还说 requierd),您将需要遍历 $scope.myFormName.mySpecialPatternField.$error

对象来获取密钥,然后将它们中的每一个都设置为 true;对于每个错误,它将设置$valid为 true,因此表单也将设置为 true $valid = true

但是,这可能在您的版本中可用,也可能不可用。查看...

于 2015-01-14T17:18:40.883 回答