Skip to content

Commit

Permalink
fix(select): auto-select new option that is marked as selected
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lgalfaso committed Jul 8, 2014
1 parent 36831ec commit b8ae73e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,20 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
};


self.addOption = function(value) {
self.addOption = function(value, element) {
assertNotHasOwnProperty(value, '"option value"');
optionsMap[value] = true;

if (ngModelCtrl.$viewValue == value) {
$element.val(value);
if (unknownOption.parent()) unknownOption.remove();
}
// Workaround for https://code.google.com/p/chromium/issues/detail?id=381459
// Adding an <option selected="selected"> element to a <select required="required"> should
// automatically select the new element
if (element[0].hasAttribute('selected')) {
element[0].selected = true;
}
};


Expand Down Expand Up @@ -625,10 +631,10 @@ var optionDirective = ['$interpolate', function($interpolate) {
if (oldVal !== newVal) {
selectCtrl.removeOption(oldVal);
}
selectCtrl.addOption(newVal);
selectCtrl.addOption(newVal, element);
});
} else {
selectCtrl.addOption(attr.value);
selectCtrl.addOption(attr.value, element);
}

element.on('$destroy', function() {
Expand Down

0 comments on commit b8ae73e

Please sign in to comment.