如何重置在形式上$脏形式上

2023-09-13 04:33:27 作者:战地残阳,断刃血淌。

我在使用的 $脏在我的申请表面临的一个问题。

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

问题是,一旦你改变表单字段的值 $脏将设置为真正但现在,当您撤消更改它不是重置的 $脏值假

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.

我们可以重新设置的 $脏值假手动但事后当你再次更改表单字段值的 $脏并不会改变它的值设置为真正。据我观察它通过把NG-脏类设置的 $脏每场表单中的价值。即使你删除它的类不影响 $脏的行为。

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

当在状态页面上的表单第一次加载它被称为质朴。你会发现,表单。$质朴真实,形式,$脏是假的。一旦有任何改变,以一个具有约束力的角度任何元素制成,这些价值观被颠倒。虽然你可以在表单输入值重置到原来的状态,这样做不会改变任何棱角状态。拨打电话,形成$ 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.