Skip to content

Commit 82d8f68

Browse files
committed
fix(select): auto-select new option that is marked as selected
When adding a new <option> element, if the DOM of this option element states that the element is marked as `selected`, then select the new <option> element Closes angular#6828
1 parent 1192531 commit 82d8f68

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/ng/directive/select.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,17 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
163163
};
164164

165165

166-
self.addOption = function(value) {
166+
self.addOption = function(value, element) {
167167
assertNotHasOwnProperty(value, '"option value"');
168168
optionsMap[value] = true;
169169

170170
if (ngModelCtrl.$viewValue == value) {
171171
$element.val(value);
172172
if (unknownOption.parent()) unknownOption.remove();
173173
}
174+
if (element[0].hasAttribute('selected')) {
175+
element[0].selected = true;
176+
}
174177
};
175178

176179

@@ -622,10 +625,10 @@ var optionDirective = ['$interpolate', function($interpolate) {
622625
scope.$watch(interpolateFn, function interpolateWatchAction(newVal, oldVal) {
623626
attr.$set('value', newVal);
624627
if (newVal !== oldVal) selectCtrl.removeOption(oldVal);
625-
selectCtrl.addOption(newVal);
628+
selectCtrl.addOption(newVal, element);
626629
});
627630
} else {
628-
selectCtrl.addOption(attr.value);
631+
selectCtrl.addOption(attr.value, element);
629632
}
630633

631634
element.on('$destroy', function() {

0 commit comments

Comments
 (0)