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

Commit 395f3ec

Browse files
petebacondarwinNarretz
authored andcommitted
fix(ngOptions): skip comments and empty options when looking for options
Related #12952 Closes #12190 Closes #13029 Closes #13033
1 parent 964a901 commit 395f3ec

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/ng/directive/ngOptions.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,15 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
635635
var emptyOption_ = emptyOption && emptyOption[0];
636636
var unknownOption_ = unknownOption && unknownOption[0];
637637

638+
// We cannot rely on the extracted empty option being the same as the compiled empty option,
639+
// because the compiled empty option might have been replaced by a comment because
640+
// it had an "element" transclusion directive on it (such as ngIf)
638641
if (emptyOption_ || unknownOption_) {
639642
while (current &&
640643
(current === emptyOption_ ||
641-
current === unknownOption_)) {
644+
current === unknownOption_ ||
645+
current.nodeType === NODE_TYPE_COMMENT ||
646+
current.value === '')) {
642647
current = current.nextSibling;
643648
}
644649
}

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)