mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-26 13:05:35 +08:00
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
This commit is contained in:
@@ -206,6 +206,42 @@ describe('form', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('rollback view value', function () {
|
||||
it('should trigger rollback on form controls', function() {
|
||||
var form = $compile(
|
||||
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
|
||||
'<input type="text" ng-model="name" />' +
|
||||
'<button ng-click="test.$rollbackViewValue()" />' +
|
||||
'</form>')(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(
|
||||
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
|
||||
'<div class="ng-form" name="child">' +
|
||||
'<input type="text" ng-model="name" />' +
|
||||
'</div>' +
|
||||
'<button ng-click="test.$rollbackViewValue()" />' +
|
||||
'</form>')(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() {
|
||||
|
||||
Reference in New Issue
Block a user