Skip to content

Commit 0b874c0

Browse files
committed
fix(ngOptions): allow option with ngIf to be removed and re-added
Fix included from angular#15812
1 parent ff0e611 commit 0b874c0

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

src/ng/directive/ngOptions.js

Lines changed: 9 additions & 16 deletions
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.
@@ -568,8 +569,12 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
568569
ngModelCtrl.$render();
569570

570571
optionEl.on('$destroy', function() {
572+
var needsRerender = selectCtrl.isEmptyOptionSelected();
573+
571574
selectCtrl.hasEmptyOption = false;
572575
selectCtrl.emptyOption = undefined;
576+
577+
if (needsRerender) ngModelCtrl.$render();
573578
});
574579
}
575580
};
@@ -582,8 +587,6 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
582587

583588
}
584589

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

648651
var groupElementMap = {};
649652

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-
660653
options.items.forEach(function addOption(option) {
661654
var groupElement;
662655

test/ng/directive/ngOptionsSpec.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,18 +2536,15 @@ describe('ngOptions', function() {
25362536
scope.isBlank = true;
25372537
});
25382538

2539-
expect(element.find('option').length).toBe(2);
2540-
option = element.find('option').eq(0);
2541-
expect(option.val()).toBe('');
2542-
expect(option.text()).toBe('blank');
2539+
expect(element).toEqualSelect([''], 'object:4');
25432540

2544-
scope.$apply(function() {
2545-
scope.isBlank = false;
2546-
});
2541+
scope.$apply('isBlank = false');
25472542

2548-
expect(element.find('option').length).toBe(1);
2549-
option = element.find('option').eq(0);
2550-
expect(option.text()).toBe('A');
2543+
expect(element).toEqualSelect(['?'], 'object:4');
2544+
2545+
scope.$apply('isBlank = true');
2546+
2547+
expect(element).toEqualSelect([''], 'object:4');
25512548
});
25522549

25532550

@@ -2567,9 +2564,7 @@ describe('ngOptions', function() {
25672564

25682565
scope.$apply('isBlank = false');
25692566

2570-
options = element.find('option');
2571-
expect(options.length).toBe(1);
2572-
expect(options.eq(0).text()).toBe('A');
2567+
expect(element).toEqualSelect(['?'], 'object:3');
25732568
}
25742569
);
25752570

0 commit comments

Comments
 (0)