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

Commit 9c9c6b3

Browse files
shahatalgalfaso
authored andcommitted
fix(ngModelOptions): allow sharing options between multiple inputs
Closes #10667
1 parent 51d6774 commit 9c9c6b3

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ng/directive/ngModel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ var ngModelOptionsDirective = function() {
12151215
restrict: 'A',
12161216
controller: ['$scope', '$attrs', function($scope, $attrs) {
12171217
var that = this;
1218-
this.$options = $scope.$eval($attrs.ngModelOptions);
1218+
this.$options = angular.copy($scope.$eval($attrs.ngModelOptions));
12191219
// Allow adding/overriding bound events
12201220
if (this.$options.updateOn !== undefined) {
12211221
this.$options.updateOnDefault = false;

test/ng/directive/ngModelSpec.js

+28
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,34 @@ describe('ngModelOptions attributes', function() {
17331733
});
17341734

17351735

1736+
it('should allow sharing options between multiple inputs', function() {
1737+
$rootScope.options = {updateOn: 'default'};
1738+
var inputElm = helper.compileInput(
1739+
'<input type="text" ng-model="name1" name="alias1" ' +
1740+
'ng-model-options="options"' +
1741+
'/>' +
1742+
'<input type="text" ng-model="name2" name="alias2" ' +
1743+
'ng-model-options="options"' +
1744+
'/>');
1745+
1746+
helper.changeGivenInputTo(inputElm.eq(0), 'a');
1747+
helper.changeGivenInputTo(inputElm.eq(1), 'b');
1748+
expect($rootScope.name1).toEqual('a');
1749+
expect($rootScope.name2).toEqual('b');
1750+
});
1751+
1752+
1753+
it('should hold a copy of the options object', function() {
1754+
$rootScope.options = {updateOn: 'default'};
1755+
var inputElm = helper.compileInput(
1756+
'<input type="text" ng-model="name" name="alias" ' +
1757+
'ng-model-options="options"' +
1758+
'/>');
1759+
expect($rootScope.options).toEqual({updateOn: 'default'});
1760+
expect($rootScope.form.alias.$options).not.toBe($rootScope.options);
1761+
});
1762+
1763+
17361764
it('should allow overriding the model update trigger event on checkboxes', function() {
17371765
var inputElm = helper.compileInput(
17381766
'<input type="checkbox" ng-model="checkbox" ' +

0 commit comments

Comments
 (0)