fix(ngOptions): allow empty option to be removed and re-added

This bug was reported as part of angular/angular.js#15801
This commit is contained in:
Pol Bonastre
2017-03-17 19:00:54 +01:00
committed by Martin Staffa
parent f90b48d7d8
commit 2fdfbe7296
2 changed files with 9 additions and 16 deletions
+5 -16
View File
@@ -429,6 +429,9 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
}
}
// The empty option will be compiled and rendered before we first generate the options
selectElement.empty();
var providedEmptyOption = !!selectCtrl.emptyOption;
var unknownOption = jqLite(optionTemplate.cloneNode(false));
@@ -545,13 +548,11 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
if (providedEmptyOption) {
// we need to remove it before calling selectElement.empty() because otherwise IE will
// remove the label from the element. wtf?
selectCtrl.emptyOption.remove();
// compile the element since there might be bindings in it
$compile(selectCtrl.emptyOption)(scope);
selectElement.prepend(selectCtrl.emptyOption);
if (selectCtrl.emptyOption[0].nodeType === NODE_TYPE_COMMENT) {
// This means the empty option has currently no actual DOM node, probably because
// it has been modified by a transclusion directive.
@@ -583,8 +584,6 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
}
selectElement.empty();
// We need to do this here to ensure that the options object is defined
// when we first hit it in writeNgOptionsValue
updateOptions();
@@ -649,16 +648,6 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
var groupElementMap = {};
// Ensure that the empty option is always there if it was explicitly provided
if (providedEmptyOption) {
if (selectCtrl.unknownOption.parent().length) {
selectCtrl.unknownOption.after(selectCtrl.emptyOption);
} else {
selectElement.prepend(selectCtrl.emptyOption);
}
}
options.items.forEach(function addOption(option) {
var groupElement;
+4
View File
@@ -2548,6 +2548,10 @@ describe('ngOptions', function() {
expect(element.find('option').length).toBe(1);
option = element.find('option').eq(0);
expect(option.text()).toBe('A');
scope.$apply('isBlank = true');
expect(element).toEqualSelect([''], 'object:4');
});