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

Commit

Permalink
fix(select): keep ngModel when selected option is recreated by ngRepeat
Browse files Browse the repository at this point in the history
Fixes #15630 
Closes #15632
  • Loading branch information
Narretz committed Jan 26, 2017
1 parent 641e13a commit 89f3e3b
Show file tree
Hide file tree
Showing 2 changed files with 35 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
34 changes: 34 additions & 0 deletions test/ng/directive/selectSpec.js
Expand Up @@ -2316,6 +2316,40 @@ describe('select', function() {

});

it('should keep the ngModel value when the selected option is recreated by ngRepeat', function() {
scope.options = [{ name: 'A'}, { name: 'B'}, { name: 'C'}];
scope.obj = {
value: 'B'
};

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

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

scope.$apply(function() {
// Only when new objects are used, ngRepeat re-creates the element from scratch
scope.options = [{ name: 'B'}, { name: 'C'}, { name: 'D'}];
});

var previouslySelectedOptionElement = optionElements[1];
optionElements = element.find('option');

expect(optionElements.length).toEqual(3);
expect(optionElements[0].value).toBe('B');
expect(optionElements[0]).toBeMarkedAsSelected();
expect(scope.obj.value).toBe('B');
// Ensure the assumption that the element is re-created is true
expect(previouslySelectedOptionElement).not.toBe(optionElements[0]);
});

});


Expand Down

0 comments on commit 89f3e3b

Please sign in to comment.