在Angular中取消提交表单 [英] Unsubmit a form in Angular

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

问题描述

我需要一个表单才能同时 $ dirty 而不是 $ submitted 我的验证工作。

I need a form to be able to be both $dirty and not $submitted for my validation to work.

我正在尝试通过CSS使用 .ng-invalid .ng-dirty 和 .ng-submitted 通过angular动态添加的类。这意味着我不能简单地在表单上将 $ submitted 设置为false,因为这不会删除关联的类。我也不能使用 $ setPristine $ setUntouched ,因为这些会删除 $ dirty flag及其类。

I'm trying to accomplish this through CSS using the .ng-invalid, .ng-dirty, and .ng-submitted classes that get dynamically added by angular. This means I cannot simply set $submitted to false on the form as this will not remove the associated class. Nor can I use $setPristine or $setUntouched as these will remove the $dirty flag and its class.

有没有办法将表单设置为未提交,以便 .ng- submit 类已删除且表单仍然是脏的?

Is there some way to set a form as unsubmitted so that the .ng-submitted class is removed AND the form remains dirty?

推荐答案

您可以使用 $ setPristine 后跟 $ setDirty 。如果您想保留 $ dirty 值,请在将其设置为pristine之前保存。

You can use $setPristine followed by $setDirty. If you want to retain the $dirty value, save it before setting it to pristine.

var formWasDirty = form.$dirty;

form.$setPristine(); 

if (formWasDirty) { 
  form.$setDirty();
}

http://plnkr.co/edit/nCgu7eBWAXwO3xeYTa6V?p=preview

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

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