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

Commit 4627410

Browse files
committed
fix(select): update option labels when model changes
Closes #9025
1 parent 7c60264 commit 4627410

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/ng/directive/select.js

+12
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,18 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
415415
ctrl.$render = render;
416416

417417
scope.$watchCollection(valuesFn, scheduleRendering);
418+
scope.$watchCollection(function () {
419+
var locals = {},
420+
values = valuesFn(scope);
421+
if (values) {
422+
var toDisplay = new Array(values.length);
423+
for (var i = 0, ii = values.length; i < ii; i++) {
424+
locals[valueName] = values[i];
425+
toDisplay[i] = displayFn(scope, locals);
426+
}
427+
return toDisplay;
428+
}
429+
}, scheduleRendering);
418430

419431
if (multiple) {
420432
scope.$watchCollection(function() { return ctrl.$modelValue; }, scheduleRendering);

test/ng/directive/selectSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,26 @@ describe('select', function() {
871871
expect(element.val()).toEqual('1');
872872
});
873873

874+
it('should update options in the DOM', function() {
875+
compile(
876+
'<select ng-model="selected" ng-options="item.id as item.name for item in values"></select>'
877+
);
878+
879+
scope.$apply(function() {
880+
scope.values = [{id: 10, name: 'A'}, {id: 20, name: 'B'}];
881+
scope.selected = scope.values[0].id;
882+
});
883+
884+
scope.$apply(function() {
885+
scope.values[0].name = 'C';
886+
});
887+
888+
var options = element.find('option');
889+
expect(options.length).toEqual(2);
890+
expect(sortedHtml(options[0])).toEqual('<option value="0">C</option>');
891+
expect(sortedHtml(options[1])).toEqual('<option value="1">B</option>');
892+
});
893+
874894

875895
it('should bind to object key', function() {
876896
createSelect({

0 commit comments

Comments
 (0)