Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 2fdfbe7

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

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
@@ -429,6 +429,9 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
429429
}
430430
}
431431

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

434437
var unknownOption = jqLite(optionTemplate.cloneNode(false));
@@ -545,13 +548,11 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
545548

546549
if (providedEmptyOption) {
547550

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

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

584585
}
585586

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

650649
var groupElementMap = {};
651650

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

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)