Skip to content

Commit 848944b

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. Ensured the placeholder, if available, is explicitly selected if the model value can not be found in the option list. Without these fixes it's up to the browser implementation to decide which option to choose. 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. Closes angular#2150, angular#1826
1 parent d1b49e2 commit 848944b

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/ng/directive/select.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,6 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
394394

395395
if (multiple) {
396396
selectedSet = new HashMap(modelValue);
397-
} else if (modelValue === null || nullOption) {
398-
// if we are not multiselect, and we are null then we have to add the nullOption
399-
optionGroups[''].push({selected:modelValue === null, id:'', label:''});
400-
selectedSet = true;
401397
}
402398

403399
// We now build up the list of options we need (we merge later)
@@ -422,9 +418,14 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
422418
selected: selected // determine if we should be selected
423419
});
424420
}
425-
if (!multiple && !selectedSet) {
426-
// nothing was selected, we have to insert the undefined item
427-
optionGroups[''].unshift({id:'?', label:'', selected:true});
421+
if (!multiple) {
422+
if (nullOption || modelValue === null) {
423+
// insert null option if we have a placeholder, or the model is null
424+
optionGroups[''].unshift({id:'', label:'', selected:!selectedSet});
425+
} else if (!selectedSet) {
426+
// option could not be found, we have to insert the undefined item
427+
optionGroups[''].unshift({id:'?', label:'', selected:true});
428+
}
428429
}
429430

430431
// Now we need to update the list of DOM nodes to match the optionGroups we computed above
@@ -468,7 +469,8 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
468469
if (existingOption.id !== option.id) {
469470
lastElement.val(existingOption.id = option.id);
470471
}
471-
if (existingOption.element.selected !== option.selected) {
472+
// lastElement.prop('selected') provided by jQuery has side-effects
473+
if (lastElement[0].selected !== option.selected) {
472474
lastElement.prop('selected', (existingOption.selected = option.selected));
473475
}
474476
} else {

test/ng/directive/selectSpec.js

+28
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 be selected, if it is available and no other option is selected', function() {
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 blank 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

@@ -1099,6 +1112,21 @@ describe('select', function() {
10991112
browserTrigger(element, 'change');
11001113
expect(scope.selected).toEqual(['0']);
11011114
});
1115+
1116+
it('should deselect all options when model is emptied', function() {
1117+
createMultiSelect();
1118+
scope.$apply(function() {
1119+
scope.values = [{name: 'A'}, {name: 'B'}];
1120+
scope.selected = [scope.values[0]];
1121+
});
1122+
expect(element.find('option')[0].selected).toEqual(true);
1123+
1124+
scope.$apply(function() {
1125+
scope.selected.pop();
1126+
});
1127+
1128+
expect(element.find('option')[0].selected).toEqual(false);
1129+
})
11021130
});
11031131

11041132

0 commit comments

Comments
 (0)