Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In AngularJS, what's the difference between ng-pristine and ng-dirty?

Tags:

angularjs

What are the differences between ng-pristine and ng-dirty? It seems you can have both to be true:

$scope.myForm.$pristine = true; // after editing the form 
like image 442
synergetic Avatar asked Aug 06 '13 06:08

synergetic


People also ask

What does dirty mean angular?

When the user changes the value in the watched field, the control is marked as "dirty" When the user blurs the form control element, the control is marked as "touched"

What is the difference between touched and dirty?

The difference between touched and dirty is that with touched the user doesn't need to actually change the value of the input control. touched is true of the field has been touched by the user, otherwise it's false. The opposite of touched is the property untouched .

What is $dirty in Angularjs?

$dirty means the user has changed the input value, $invalid means the address itself is invalid. Therefore the error is only shown if the user has actively changed the input value to either an empty or invalid value.

What does it mean for a form to be dirty?

pristine: This property returns true if the element's contents have not been changed. - dirty: This property returns true if the element's contents have been changed. -


1 Answers

The ng-dirty class tells you that the form has been modified by the user, whereas the ng-pristine class tells you that the form has not been modified by the user. So ng-dirty and ng-pristine are two sides of the same story.

The classes are set on any field, while the form has two properties, $dirty and $pristine.

You can use the $scope.form.$setPristine() function to reset a form to pristine state (please note that this is an AngularJS 1.1.x feature).

If you want a $scope.form.$setPristine()-ish behavior even in 1.0.x branch of AngularJS, you need to roll your own solution (some pretty good ones can be found here). Basically, this means iterating over all form fields and setting their $dirty flag to false.

Hope this helps.

like image 134
Golo Roden Avatar answered Sep 18 '22 13:09

Golo Roden