Skip to content

Commit c9b77eb

Browse files
pbr1111Narretz
authored andcommitted
fix(ngOptions): allow empty option to be removed and re-added
This bug was reported as part of angular#15801
1 parent ff0e611 commit c9b77eb

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

src/ng/directive/ngOptions.js

+5-16
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
428428
}
429429
}
430430

431+
// The empty option will be compiled and rendered before we first generate the options
432+
selectElement.empty();
433+
431434
var providedEmptyOption = !!selectCtrl.emptyOption;
432435

433436
var unknownOption = jqLite(optionTemplate.cloneNode(false));
@@ -544,13 +547,11 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
544547

545548
if (providedEmptyOption) {
546549

547-
// we need to remove it before calling selectElement.empty() because otherwise IE will
548-
// remove the label from the element. wtf?
549-
selectCtrl.emptyOption.remove();
550-
551550
// compile the element since there might be bindings in it
552551
$compile(selectCtrl.emptyOption)(scope);
553552

553+
selectElement.prepend(selectCtrl.emptyOption);
554+
554555
if (selectCtrl.emptyOption[0].nodeType === NODE_TYPE_COMMENT) {
555556
// This means the empty option has currently no actual DOM node, probably because
556557
// it has been modified by a transclusion directive.
@@ -582,8 +583,6 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
582583

583584
}
584585

585-
selectElement.empty();
586-
587586
// We need to do this here to ensure that the options object is defined
588587
// when we first hit it in writeNgOptionsValue
589588
updateOptions();
@@ -647,16 +646,6 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
647646

648647
var groupElementMap = {};
649648

650-
// Ensure that the empty option is always there if it was explicitly provided
651-
if (providedEmptyOption) {
652-
653-
if (selectCtrl.unknownOption.parent().length) {
654-
selectCtrl.unknownOption.after(selectCtrl.emptyOption);
655-
} else {
656-
selectElement.prepend(selectCtrl.emptyOption);
657-
}
658-
}
659-
660649
options.items.forEach(function addOption(option) {
661650
var groupElement;
662651

test/ng/directive/ngOptionsSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,10 @@ describe('ngOptions', function() {
25482548
expect(element.find('option').length).toBe(1);
25492549
option = element.find('option').eq(0);
25502550
expect(option.text()).toBe('A');
2551+
2552+
scope.$apply('isBlank = true');
2553+
2554+
expect(element).toEqualSelect([''], 'object:4');
25512555
});
25522556

25532557

0 commit comments

Comments
 (0)