如何在表单中重置 $dirty [英] How to reset $dirty in form

查看:27
本文介绍了如何在表单中重置 $dirty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在申请表中使用 $dirty 时遇到一个问题.

I am facing one issue while using $dirty in my application form.

问题是一旦您更改表单字段,$dirty 的值将设置为 true但是现在当您撤消更改时,它不会将 $dirty 值重置为 false.

The issue is once you change the form field,value of $dirty will get set to true but now when you undo your changes it's not resetting $dirty value to false.

我们可以手动将 $dirty 值重置为 false,但之后当您再次更改表单字段值时 $dirty 不会更改其值为 true.根据我的观察,它通过放置 ng-dirty 类来设置表单中每个字段的 $dirty 值.即使您删除该类,它也不会影响 $dirty 行为.

We can reset the $dirty value to false manually but afterwards when you again change your form field values $dirty won't change its value to true. According to my observation it set $dirty value of every field in your form by placing ng-dirty class. Even if you remove that class it is not affecting $dirty behavior.

// Please find below attached Fiddle for code reference

小提琴.

推荐答案

您正在寻找的是 $setPristine().您可以在此处的文档中找到它:http://docs.angularjs.org/api/ng/type/form.FormController

What you are looking for is $setPristine(). You'll find it in the docs here: http://docs.angularjs.org/api/ng/type/form.FormController

当表单第一次加载到页面上时,它处于一种称为原始的状态.你会发现 form.$pristine 是真的,form.$dirty 是假的.一旦对具有 Angular 绑定的任何元素进行了任何更改,这些值就会反转.虽然您可以将表单输入值重置为其原始状态,但这样做不会更改任何 Angular 表单状态.调用 form.$setPristine();将这些表单值设置回其原始状态.

When a form first loads on the page it is in a state called pristine. You'll find that form.$pristine is true and form.$dirty is false. Once any changes have been made to any element that has an Angular binding, those values are reversed. While you can reset the form input values to their original state, doing so does not change either Angular form state. Making a call to form.$setPristine(); sets those form values back to their original state.

这篇关于如何在表单中重置 $dirty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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