-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Issue #856: add ability to reset a form to its pristine state #1127
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ var nullFormCtrl = { | |
$addControl: noop, | ||
$removeControl: noop, | ||
$setValidity: noop, | ||
$setDirty: noop | ||
$setDirty: noop, | ||
$setPristine: noop | ||
}; | ||
|
||
/** | ||
|
@@ -119,6 +120,31 @@ function FormController(element, attrs) { | |
form.$pristine = false; | ||
}; | ||
|
||
/** | ||
* @ngdoc function | ||
* @name ng.directive:form.FormController#$setPristine | ||
* @methodOf ng.directive:form.FormController | ||
* | ||
* @description | ||
* Sets the form to its pristine state. | ||
* | ||
* This method can be called to remove the 'ng-dirty' class and set the form to its pristine | ||
* state (ng-pristine class). This method will also propagate to all the controls contained | ||
* in this form. | ||
* | ||
* Setting a form back to a pristine state is often useful when we want to 'reuse' a form after | ||
* saving or resetting it. | ||
*/ | ||
form.$setPristine = function () { | ||
element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS); | ||
form.$dirty = false; | ||
form.$pristine = true; | ||
angular.forEach(form, function(value, key) { | ||
if (value.$name && value.$setPristine) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you check for $name? anonymous form controls should be reset as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that anonymous form controls are not tracked as part of the form controller. Looked at the https://github.com/angular/angular.js/blob/master/src/ng/directive/form.js#L64 and the same check for If I'm correct then we don't have anonymous controls (inputs and sub-forms) on the FormController level and thus can't reset them to their pristine state. Still individual controls can be reset. We could relay on the presence of the @IgorMinar might have missed something here but at least this was reasoning. |
||
value.$setPristine(); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no
angular
, justforEach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes!