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

Commit 2546c29

Browse files
feat(ngModel): add $overrideModelOptions support
This change allows developers to modify the model options for an `ngModel` directive programmatically. Closes #15415
1 parent 19ea708 commit 2546c29

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/ng/directive/ngModel.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,29 @@ NgModelController.prototype = {
829829
that.$commitViewValue();
830830
});
831831
}
832+
},
833+
834+
/**
835+
* @ngdoc method
836+
*
837+
* @name ngModel.NgModelController#$overrideModelOptions
838+
*
839+
* @description
840+
*
841+
* Override the current model options settings programmatically.
842+
*
843+
* The previous `ModelOptions` value will not be modified. Instead, a
844+
* new `ModelOptions` object will inherit from the previous one overriding
845+
* or inheriting settings that are defined in the given parameter.
846+
*
847+
* See {@link ngModelOptions} for information about what options can be specified
848+
* and how model option inheritance works.
849+
*
850+
* @param {Object} options a hash of settings to override the previous options
851+
*
852+
*/
853+
$overrideModelOptions: function(options) {
854+
this.$options = this.$options.createChild(options);
832855
}
833856
};
834857

test/ng/directive/ngModelSpec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,48 @@ describe('ngModel', function() {
13371337
});
13381338

13391339
});
1340+
1341+
describe('override ModelOptions', function() {
1342+
it('should replace the previous model options', function() {
1343+
var $options = ctrl.$options;
1344+
ctrl.$overrideModelOptions({});
1345+
expect(ctrl.$options).not.toBe($options);
1346+
});
1347+
1348+
it('should set the given options', function() {
1349+
var $options = ctrl.$options;
1350+
ctrl.$overrideModelOptions({ debounce: 1000, updateOn: 'blur' });
1351+
expect(ctrl.$options.getOption('debounce')).toEqual(1000);
1352+
expect(ctrl.$options.getOption('updateOn')).toEqual('blur');
1353+
expect(ctrl.$options.getOption('updateOnDefault')).toBe(false);
1354+
});
1355+
1356+
it('should inherit from a parent model options if specified', inject(function($compile, $rootScope) {
1357+
var element = $compile(
1358+
'<form name="form" ng-model-options="{debounce: 1000, updateOn: \'blur\'}">' +
1359+
' <input ng-model="value" name="input">' +
1360+
'</form>')($rootScope);
1361+
var ctrl = $rootScope.form.input;
1362+
ctrl.$overrideModelOptions({ debounce: 2000, '*': '$inherit' });
1363+
expect(ctrl.$options.getOption('debounce')).toEqual(2000);
1364+
expect(ctrl.$options.getOption('updateOn')).toEqual('blur');
1365+
expect(ctrl.$options.getOption('updateOnDefault')).toBe(false);
1366+
dealoc(element);
1367+
}));
1368+
1369+
it('should not inherit from a parent model options if not specified', inject(function($compile, $rootScope) {
1370+
var element = $compile(
1371+
'<form name="form" ng-model-options="{debounce: 1000, updateOn: \'blur\'}">' +
1372+
' <input ng-model="value" name="input">' +
1373+
'</form>')($rootScope);
1374+
var ctrl = $rootScope.form.input;
1375+
ctrl.$overrideModelOptions({ debounce: 2000 });
1376+
expect(ctrl.$options.getOption('debounce')).toEqual(2000);
1377+
expect(ctrl.$options.getOption('updateOn')).toEqual('');
1378+
expect(ctrl.$options.getOption('updateOnDefault')).toBe(true);
1379+
dealoc(element);
1380+
}));
1381+
});
13401382
});
13411383

13421384

0 commit comments

Comments
 (0)