Skip to content

Commit fb1130a

Browse files
fix(ngOptions) skip authentic empty options when looking for options
Related angular#12952 Closes angular#12190 Closes angular#13029
1 parent 4ad0ca1 commit fb1130a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/ng/directive/ngOptions.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
408408
}
409409
}
410410

411-
var providedEmptyOption = !!emptyOption;
411+
var providedEmptyOption = emptyOption;
412412

413413
var unknownOption = jqLite(optionTemplate.cloneNode(false));
414414
unknownOption.val('?');
@@ -618,10 +618,19 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
618618
var emptyOption_ = emptyOption && emptyOption[0];
619619
var unknownOption_ = unknownOption && unknownOption[0];
620620

621+
// If the compiled empty option was replaced by a comment because
622+
// it had an "element" transclusion directive on it (such as ngIf)
623+
// then compare against the pre-compiled empty element instead
624+
if (emptyOption && emptyOption_.nodeType === NODE_TYPE_COMMENT) {
625+
emptyOption_ = providedEmptyOption[0];
626+
}
627+
621628
if (emptyOption_ || unknownOption_) {
622629
while (current &&
623630
(current === emptyOption_ ||
624-
current === unknownOption_)) {
631+
current === unknownOption_ ||
632+
current.nodeType === NODE_TYPE_COMMENT ||
633+
current.value === '')) {
625634
current = current.nextSibling;
626635
}
627636
}

test/ng/directive/ngOptionsSpec.js

+46
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,52 @@ describe('ngOptions', function() {
21442144
});
21452145

21462146

2147+
it('should be possible to use ngIf in the blank option', function() {
2148+
var option;
2149+
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');
2150+
2151+
scope.$apply(function() {
2152+
scope.values = [{name: 'A'}];
2153+
scope.isBlank = true;
2154+
});
2155+
2156+
expect(element.find('option').length).toBe(2);
2157+
option = element.find('option').eq(0);
2158+
expect(option.val()).toBe('');
2159+
expect(option.text()).toBe('blank');
2160+
2161+
scope.$apply(function() {
2162+
scope.isBlank = false;
2163+
});
2164+
2165+
expect(element.find('option').length).toBe(1);
2166+
option = element.find('option').eq(0);
2167+
expect(option.text()).toBe('A');
2168+
});
2169+
2170+
2171+
it('should be possible to use ngIf in the blank option when values are available upon linking',
2172+
function() {
2173+
var options;
2174+
2175+
scope.values = [{name: 'A'}];
2176+
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');
2177+
2178+
scope.$apply('isBlank = true');
2179+
2180+
options = element.find('option');
2181+
expect(options.length).toBe(2);
2182+
expect(options.eq(0).val()).toBe('');
2183+
expect(options.eq(0).text()).toBe('blank');
2184+
2185+
scope.$apply('isBlank = false');
2186+
2187+
options = element.find('option');
2188+
expect(options.length).toBe(1);
2189+
expect(options.eq(0).text()).toBe('A');
2190+
}
2191+
);
2192+
21472193
it('should not throw when a directive compiles the blank option before ngOptions is linked', function() {
21482194
expect(function() {
21492195
createSelect({

0 commit comments

Comments
 (0)