Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset $dirty in form

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.

like image 708
pixelbyaj Avatar asked Apr 10 '14 14:04

pixelbyaj


People also ask

How do you reset the template driven form?

Resetting a form in template-driven approach: In template driven approach, we need to import NgForm from '@angular/forms' and we use [(ngModel)] directive for two way data-binding and we should also import FormsModule from '@angular/forms' in app. module. ts file.

How would you reset all objects on a form in angular?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.

How do you set pristine to false?

Using $setPristine will set the $pristine property of the form to true and $dirty to false.


1 Answers

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.

like image 162
MBielski Avatar answered Oct 18 '22 20:10

MBielski