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

Commit 131af82

Browse files
committed
fix(select): keep ngModel when selected option is recreated by ngRepeat
Fixes #15630 Closes #15632
1 parent c219a46 commit 131af82

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/ng/directive/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ var SelectController =
281281
var removeValue = optionAttrs.value;
282282

283283
self.removeOption(removeValue);
284-
self.ngModelCtrl.$render();
284+
scheduleRender();
285285

286286
if (self.multiple && currentValue && currentValue.indexOf(removeValue) !== -1 ||
287287
currentValue === removeValue

test/ng/directive/selectSpec.js

+34
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,40 @@ describe('select', function() {
23162316

23172317
});
23182318

2319+
it('should keep the ngModel value when the selected option is recreated by ngRepeat', function() {
2320+
scope.options = [{ name: 'A'}, { name: 'B'}, { name: 'C'}];
2321+
scope.obj = {
2322+
value: 'B'
2323+
};
2324+
2325+
compile(
2326+
'<select ng-model="obj.value">' +
2327+
'<option ng-repeat="option in options" value="{{option.name}}">{{option.name}}</option>' +
2328+
'</select>'
2329+
);
2330+
2331+
var optionElements = element.find('option');
2332+
expect(optionElements.length).toEqual(3);
2333+
expect(optionElements[0].value).toBe('A');
2334+
expect(optionElements[1]).toBeMarkedAsSelected();
2335+
expect(scope.obj.value).toBe('B');
2336+
2337+
scope.$apply(function() {
2338+
// Only when new objects are used, ngRepeat re-creates the element from scratch
2339+
scope.options = [{ name: 'B'}, { name: 'C'}, { name: 'D'}];
2340+
});
2341+
2342+
var previouslySelectedOptionElement = optionElements[1];
2343+
optionElements = element.find('option');
2344+
2345+
expect(optionElements.length).toEqual(3);
2346+
expect(optionElements[0].value).toBe('B');
2347+
expect(optionElements[0]).toBeMarkedAsSelected();
2348+
expect(scope.obj.value).toBe('B');
2349+
// Ensure the assumption that the element is re-created is true
2350+
expect(previouslySelectedOptionElement).not.toBe(optionElements[0]);
2351+
});
2352+
23192353
});
23202354

23212355

0 commit comments

Comments
 (0)