大佬教程收集整理的这篇文章主要介绍了在AngularJS中,当同一形式的另一个值发生变化时,如何强制重新验证表单中的字段?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<SELEct id="planet" class="form-control" name="planet" ng-model="planet" ng-options="c.val as c.label for c in planets"></SELEct>
在输入中,我通过名为“input-validation”的自定义指令应用自定义验证:
<input id="city" input-validation iv-allow-if="planet==='earth'" class="form-control" name="city" ng-model="city" required>
这是指令:
.directive('inputValidation',[function() { return { require: 'ngModel',reStrict: 'A',scope: { ivAllowIf: '=' },link: function(scope,elm,attrs,ctrl) { ctrl.$parserS.Unshift(function(viewvalue) { //input is allowed if the attribute is not present or the expression evaluates to true var inputAllowed = attrs.ivAllowIf === undefined || scope.$parent.$eval(attrs.ivAllowIf); if (inputAllowed) { ctrl.$SETVALidity('iv',truE); return viewValue; } else { ctrl.$SETVALidity('iv',falsE); return undefined; } }); } }; }])
可以在PLnkr:http://plnkr.co/edit/t2xMPy1ehVFA5KNEDfrf?p=preview中检查完整的示例
scope.$watch(function () { return $parse(attrs.endDatE)(scopE); },function () { ngModel.$validate(); });
要采取的重要部分是在指令中调用ngModel.$validate().
注意
您应该使用$validators进行上述自定义验证. read here,$parsers是旧方法 – 来自angularjs 1.3使用$validators
以上是大佬教程为你收集整理的在AngularJS中,当同一形式的另一个值发生变化时,如何强制重新验证表单中的字段?全部内容,希望文章能够帮你解决在AngularJS中,当同一形式的另一个值发生变化时,如何强制重新验证表单中的字段?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。