Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 85b7731

Browse files
shahatarodyhaddad
authored andcommitted
feat(FormController): add $rollbackViewValue to rollback all controls
Currently it is possible to use `ngModelOptions` to pend model updates until form is submitted, but in case the user wants to reset the form back to its original values he must call `$rollbackViewValue` on each input control in the form. This commit adds a `$rollbackViewValue` on the form controller in order to make this operation easier, similarly to `$commitViewValue`. Closes #7595
1 parent d2963ad commit 85b7731

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/ng/directive/form.js

+17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ function FormController(element, attrs, $scope, $animate) {
7474
$animate.addClass(element, (isValid ? VALID_CLASS : INVALID_CLASS) + validationErrorKey);
7575
}
7676

77+
/**
78+
* @ngdoc method
79+
* @name form.FormController#$rollbackViewValue
80+
*
81+
* @description
82+
* Rollback all form controls pending updates to the `$modelValue`.
83+
*
84+
* Updates may be pending by a debounced event or because the input is waiting for a some future
85+
* event defined in `ng-model-options`. This method is typically needed by the reset button of
86+
* a form that uses `ng-model-options` to pend updates.
87+
*/
88+
form.$rollbackViewValue = function() {
89+
forEach(controls, function(control) {
90+
control.$rollbackViewValue();
91+
});
92+
};
93+
7794
/**
7895
* @ngdoc method
7996
* @name form.FormController#$commitViewValue

test/ng/directive/formSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,42 @@ describe('form', function() {
206206
});
207207
});
208208

209+
describe('rollback view value', function () {
210+
it('should trigger rollback on form controls', function() {
211+
var form = $compile(
212+
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
213+
'<input type="text" ng-model="name" />' +
214+
'<button ng-click="test.$rollbackViewValue()" />' +
215+
'</form>')(scope);
216+
scope.$digest();
217+
218+
var inputElm = form.find('input').eq(0);
219+
changeInputValue(inputElm, 'a');
220+
expect(inputElm.val()).toBe('a');
221+
browserTrigger(form.find('button'), 'click');
222+
expect(inputElm.val()).toBe('');
223+
dealoc(form);
224+
});
225+
226+
it('should trigger rollback on form controls with nested forms', function() {
227+
var form = $compile(
228+
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
229+
'<div class="ng-form" name="child">' +
230+
'<input type="text" ng-model="name" />' +
231+
'</div>' +
232+
'<button ng-click="test.$rollbackViewValue()" />' +
233+
'</form>')(scope);
234+
scope.$digest();
235+
236+
var inputElm = form.find('input').eq(0);
237+
changeInputValue(inputElm, 'a');
238+
expect(inputElm.val()).toBe('a');
239+
browserTrigger(form.find('button'), 'click');
240+
expect(inputElm.val()).toBe('');
241+
dealoc(form);
242+
});
243+
});
244+
209245
describe('preventing default submission', function() {
210246

211247
it('should prevent form submission', function() {

0 commit comments

Comments
 (0)