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

fix(ngModelOptions): allow sharing options between multiple inputs #10673

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ var ngModelOptionsDirective = function() {
restrict: 'A',
controller: ['$scope', '$attrs', function($scope, $attrs) {
var that = this;
this.$options = $scope.$eval($attrs.ngModelOptions);
this.$options = angular.copy($scope.$eval($attrs.ngModelOptions));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really minor, and a bit late, but this should just be copy...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, should be able to make the change in about an hour (unless
someone beats me)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done and thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks & sorry. I always keep forgetting not to use angular.* :(

// Allow adding/overriding bound events
if (this.$options.updateOn !== undefined) {
this.$options.updateOnDefault = false;
Expand Down
28 changes: 28 additions & 0 deletions test/ng/directive/ngModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,34 @@ describe('ngModelOptions attributes', function() {
});


it('should allow sharing options between multiple inputs', function() {
$rootScope.options = {updateOn: 'default'};
var inputElm = helper.compileInput(
'<input type="text" ng-model="name1" name="alias1" ' +
'ng-model-options="options"' +
'/>' +
'<input type="text" ng-model="name2" name="alias2" ' +
'ng-model-options="options"' +
'/>');

helper.changeGivenInputTo(inputElm.eq(0), 'a');
helper.changeGivenInputTo(inputElm.eq(1), 'b');
expect($rootScope.name1).toEqual('a');
expect($rootScope.name2).toEqual('b');
});


it('should hold a copy of the options object', function() {
$rootScope.options = {updateOn: 'default'};
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="options"' +
'/>');
expect($rootScope.options).toEqual({updateOn: 'default'});
expect($rootScope.form.alias.$options).not.toBe($rootScope.options);
});


it('should allow overriding the model update trigger event on checkboxes', function() {
var inputElm = helper.compileInput(
'<input type="checkbox" ng-model="checkbox" ' +
Expand Down