本文介绍了提交后,AngularJS验证ng-invalid类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的文章中,我有几个输入和验证,我需要 ng-invalid 类才能在用户提交表单后应用。

In my from I have several inputs with validation, I need for the ng-invalid class to only be applied once the user has submitted the form.

提交时,我可以在范围上设置一个值...

On submit I can set a value on the scope as such...

$scope.submitForm = function () {
    $scope.submited = true;
    // other bits
}

...但我无法弄清楚如何有条件地显示 ng-invalid 而不改变验证本身。

... but I cant figure out how to conditionally display ng-invalid without changing the validation itself.

我正在运行角度 1.1.5

推荐答案

对于自定义错误,我建议在每个字段中使用$ setValidity方法。

For custom errors I suggest to use the $setValidity method within each field.

$scope.formName.fieldName.$setValidity('custom_error_name', true);

所以你也可以更好地控制css部分,因为这种工作流程也会创建一个您的字段中的自定义类,如ng-custom_error_name,以便您可以处理。

so you will have more control over the css part too, because this kind of workflow will create also a custom class inside your field like "ng-custom_error_name" so you can deal with that.

这篇关于提交后,AngularJS验证ng-invalid类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:10