Skip to content

Commit

Permalink
fix(select): keep ngModel when selected option is recreated with ngRe…
Browse files Browse the repository at this point in the history
…peat

Fixes angular#15630
  • Loading branch information
Narretz committed Jan 26, 2017
1 parent 641e13a commit 2d7e08e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ng/directive/select.js
Expand Up @@ -281,7 +281,7 @@ var SelectController =
var removeValue = optionAttrs.value;

self.removeOption(removeValue);
self.ngModelCtrl.$render();
scheduleRender();

if (self.multiple && currentValue && currentValue.indexOf(removeValue) !== -1 ||
currentValue === removeValue
Expand Down
28 changes: 28 additions & 0 deletions test/ng/directive/selectSpec.js
Expand Up @@ -2316,6 +2316,34 @@ describe('select', function() {

});

it('should keep the ngModel value when the selected option is recreated by ngRepeat', function() {

scope.options = ['A', 'B', 'C'];
scope.obj = {
value: 'B'
};

compile(
'<select ng-model="obj.value">' +
'<option ng-repeat="option in options" value="{{option}}">{{option}}</option>' +
'</select>'
);

var optionElements = element.find('option');
expect(optionElements.length).toEqual(3);
expect(optionElements[0].value).toBe('A');
expect(optionElements[1]).toBeMarkedAsSelected();

scope.$apply(function() {
scope.options = ['B', 'C', 'D'];
});

optionElements = element.find('option');
expect(optionElements.length).toEqual(3);
expect(optionElements[0].value).toBe('B');
expect(optionElements[0]).toBeMarkedAsSelected();
});

});


Expand Down

0 comments on commit 2d7e08e

Please sign in to comment.