Skip to content

Commit 2d7e08e

Browse files
committed
fix(select): keep ngModel when selected option is recreated with ngRepeat
Fixes angular#15630
1 parent 641e13a commit 2d7e08e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-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

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

23172317
});
23182318

2319+
it('should keep the ngModel value when the selected option is recreated by ngRepeat', function() {
2320+
2321+
scope.options = ['A', 'B', 'C'];
2322+
scope.obj = {
2323+
value: 'B'
2324+
};
2325+
2326+
compile(
2327+
'<select ng-model="obj.value">' +
2328+
'<option ng-repeat="option in options" value="{{option}}">{{option}}</option>' +
2329+
'</select>'
2330+
);
2331+
2332+
var optionElements = element.find('option');
2333+
expect(optionElements.length).toEqual(3);
2334+
expect(optionElements[0].value).toBe('A');
2335+
expect(optionElements[1]).toBeMarkedAsSelected();
2336+
2337+
scope.$apply(function() {
2338+
scope.options = ['B', 'C', 'D'];
2339+
});
2340+
2341+
optionElements = element.find('option');
2342+
expect(optionElements.length).toEqual(3);
2343+
expect(optionElements[0].value).toBe('B');
2344+
expect(optionElements[0]).toBeMarkedAsSelected();
2345+
});
2346+
23192347
});
23202348

23212349

0 commit comments

Comments
 (0)