Skip to content

Commit 67a5b4a

Browse files
author
Roarke Gaskill
committed
fix(select): select doesn't work when label ngModel value match.
The selectCtrl ```addOption``` compares the label value with the ngModel value when determining when to set the value of the select element. This causes a problem if the label matches model value but not the selectAs value of the option. fixes angular#11835
1 parent 0bb57d5 commit 67a5b4a

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/ng/directive/select.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,15 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
715715
}
716716
optionGroup[0].element.remove();
717717
}
718-
forEach(labelMap, function(count, label) {
719-
if (count > 0) {
720-
selectCtrl.addOption(label);
721-
} else if (count < 0) {
722-
selectCtrl.removeOption(label);
723-
}
724-
});
718+
if (!anySelected) {
719+
forEach(labelMap, function(count, label) {
720+
if (count > 0) {
721+
selectCtrl.addOption(label);
722+
} else if (count < 0) {
723+
selectCtrl.removeOption(label);
724+
}
725+
});
726+
}
725727
}
726728
}
727729
}

test/ng/directive/selectSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,26 @@ describe('select', function() {
888888
expect(scope.selected).toBe(20);
889889
});
890890

891+
it('should support single select with array source with matching label and id', function() {
892+
createSelect({
893+
'ng-model': 'selected.id',
894+
'ng-options': 'item.id as item.label for item in arrTmp'
895+
}, true);
896+
897+
scope.arrTmp = [];
898+
var arr = [{id: 10, label: 'ten'}, {id:20, label: 'twenty'}, {id: 30, label: '10'}, {id:40, label: '20'}];
899+
900+
scope.$apply(function() {
901+
scope.selected = {id: 20};
902+
});
903+
expect(element.val()).toBe('');
904+
905+
scope.$apply(function() {
906+
scope.arrTmp = arr;
907+
});
908+
expect(element.val()).toBe('1');
909+
});
910+
891911

892912
it('should support multi select with array source', function() {
893913
createSelect({

0 commit comments

Comments
 (0)