Skip to content

Commit 20c920e

Browse files
author
Chad Smith
committed
fix(select): placeholder (empty option) is lost in IE9
Fixes a check inside render for select elements with ngOptions, which compares the selected property of an element with it's desired state. In instances where no element should be selected, this resulted in the first option in the select element having it's selected attribute set from undefined to false. In most browsers, this has the effect of displaying the first item in the list. In IE9 however, this causes the select to display nothing. In other browsers this would still cause unnecessary changes in selected state, but no visible issue would manifest. Closes angular#2150, angular#1826
1 parent d1b49e2 commit 20c920e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/ng/directive/select.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
468468
if (existingOption.id !== option.id) {
469469
lastElement.val(existingOption.id = option.id);
470470
}
471-
if (existingOption.element.selected !== option.selected) {
471+
if (!!lastElement.selected !== option.selected) {
472472
lastElement.prop('selected', (existingOption.selected = option.selected));
473473
}
474474
} else {

test/ng/directive/selectSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,19 @@ describe('select', function() {
977977
expect(option.attr('id')).toBe('road-runner');
978978
expect(option.attr('custom-attr')).toBe('custom-attr');
979979
});
980+
981+
it ('should select the null option, if it\'s available and no other option is selected', inject(function($timeout) {
982+
// selectedIndex is used here because jqLite incorrectly reports element.val()
983+
scope.$apply(function() {
984+
scope.values = [{name: 'A'}];
985+
});
986+
createSingleSelect(true);
987+
// ensure the first option (the null option) is selected
988+
expect(element[0].selectedIndex).toEqual(0);
989+
scope.$digest();
990+
// ensure the option has not changed following the digest
991+
expect(element[0].selectedIndex).toEqual(0);
992+
}));
980993
});
981994

982995

0 commit comments

Comments
 (0)