diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index 0369eb4eb2e8..64000220c1b2 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -74,6 +74,23 @@ function FormController(element, attrs, $scope, $animate) { $animate.addClass(element, (isValid ? VALID_CLASS : INVALID_CLASS) + validationErrorKey); } + /** + * @ngdoc method + * @name form.FormController#$rollbackViewValue + * + * @description + * Rollback all form controls pending updates to the `$modelValue`. + * + * Updates may be pending by a debounced event or because the input is waiting for a some future + * event defined in `ng-model-options`. This method is typically needed by the reset button of + * a form that uses `ng-model-options` to pend updates. + */ + form.$rollbackViewValue = function() { + forEach(controls, function(control) { + control.$rollbackViewValue(); + }); + }; + /** * @ngdoc method * @name form.FormController#$commitViewValue diff --git a/test/ng/directive/formSpec.js b/test/ng/directive/formSpec.js index 03829da12d07..f43c35a3713f 100644 --- a/test/ng/directive/formSpec.js +++ b/test/ng/directive/formSpec.js @@ -206,6 +206,42 @@ describe('form', function() { }); }); + describe('rollback view value', function () { + it('should trigger rollback on form controls', function() { + var form = $compile( + '
')(scope); + scope.$digest(); + + var inputElm = form.find('input').eq(0); + changeInputValue(inputElm, 'a'); + expect(inputElm.val()).toBe('a'); + browserTrigger(form.find('button'), 'click'); + expect(inputElm.val()).toBe(''); + dealoc(form); + }); + + it('should trigger rollback on form controls with nested forms', function() { + var form = $compile( + '')(scope); + scope.$digest(); + + var inputElm = form.find('input').eq(0); + changeInputValue(inputElm, 'a'); + expect(inputElm.val()).toBe('a'); + browserTrigger(form.find('button'), 'click'); + expect(inputElm.val()).toBe(''); + dealoc(form); + }); + }); + describe('preventing default submission', function() { it('should prevent form submission', function() {