触发 Angular 表单提交中所有字段的验证 [英] Trigger validation of all fields in Angular Form submit

查看:28
本文介绍了触发 Angular 表单提交中所有字段的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这种方法:http://plnkr.co/edit/A6gvyoXbBd2kfToPmiiA?p=preview 仅验证模糊字段.这很好用,但我也想在用户单击提交"按钮(不是真正的提交,而是对函数的 data-ng-click 调用)时验证它们(并因此显示这些字段的错误,如果有的话)

I'm using this method: http://plnkr.co/edit/A6gvyoXbBd2kfToPmiiA?p=preview to only validate fields on blur. This works fine, but I would also like to validate them (and thus show the errors for those fields if any) when the user clicks the 'submit' button (not a real submit but a data-ng-click call to a function)

是否有某种方法可以在单击该按钮时再次触发所有字段的验证?

Is there some way to trigger validation on all the fields again when clicking that button?

推荐答案

对我有用的是使用 $setSubmitted 函数,该函数首次出现在 1.3.20 版的 angular 文档中.

What worked for me was using the $setSubmitted function, which first shows up in the angular docs in version 1.3.20.

在我想触发验证的点击事件中,我执行了以下操作:

In the click event where I wanted to trigger the validation, I did the following:

vm.triggerSubmit = function() {
    vm.homeForm.$setSubmitted();
    ...
}

这就是我所需要的.根据文档,它将表单设置为其提交状态".此处提到了它.

That was all it took for me. According to the docs it "Sets the form to its submitted state." It's mentioned here.

这篇关于触发 Angular 表单提交中所有字段的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆