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

Commit d89d59f

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

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/ng/directive/select.js

+13
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
404404
ctrl.$render = render;
405405

406406
scope.$watchCollection(valuesFn, render);
407+
scope.$watchCollection(function () {
408+
var locals = {},
409+
values = valuesFn(scope);
410+
if (values) {
411+
var toDisplay = new Array(values.length);
412+
for (var i = 0, ii = values.length; i < ii; i++) {
413+
locals[valueName] = values[i];
414+
toDisplay[i] = displayFn(scope, locals);
415+
}
416+
return toDisplay;
417+
}
418+
}, render);
419+
407420
if ( multiple ) {
408421
scope.$watchCollection(function() { return ctrl.$modelValue; }, render);
409422
}

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)