fix(select): keep ngModel when selected option is recreated by ngRepeat

Fixes #15630 
Closes #15632
This commit is contained in:
Martin Staffa
2017-01-26 13:29:11 +01:00
committed by GitHub
parent 641e13acc1
commit 89f3e3b0af
2 changed files with 35 additions and 1 deletions
+1 -1
View File
@@ -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
+34
View File
@@ -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]);
});
});