39

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.

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

Fiddle.

1 Answer 1

69

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

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.

3
  • Would you please give me Fiddle example for the same
    – pixelbyaj
    Apr 11, 2014 at 18:40
  • Thanks @MBielski :) I'll check it my application and let you know :)
    – pixelbyaj
    Apr 13, 2014 at 3:53
  • 1
    I've seen it where I create a form, and then somehow in loading it up the $pristine state gets set to false, even though the values haven't been changed by the user. If I do a $setPristine, am I basically saying, "the way I have this form set up is to be considered pristine" or am I saying, "go through the form and change all the controls back to their original values" ?
    – Mike K
    Apr 6, 2018 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.