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

Commit 68d4dc5

Browse files
committed
fix(ngOptions): skip comments when looking for option elements
When the empty/blank option has a directive that transcludes, ngIf for example, a comment will be added into the select. Previously, ngOptions used this comment as the empty option, which would mess up the displayed options. Closes #12190
1 parent 03a4a96 commit 68d4dc5

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ng/directive/ngOptions.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,10 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
629629
if (emptyOption_ || unknownOption_) {
630630
while (current &&
631631
(current === emptyOption_ ||
632-
current === unknownOption_)) {
632+
current === unknownOption_ ||
633+
emptyOption_ && emptyOption_.nodeType === NODE_TYPE_COMMENT)) {
634+
// Empty options might have directives that transclude
635+
// and insert comments (e.g. ngIf)
633636
current = current.nextSibling;
634637
}
635638
}

test/ng/directive/ngOptionsSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,30 @@ describe('ngOptions', function() {
20862086
expect(element[0].selectedIndex).toEqual(0);
20872087
expect(scope.selected).toEqual([]);
20882088
});
2089+
2090+
2091+
it('should be possible to use ngIf in the blank option', function() {
2092+
var option;
2093+
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');
2094+
2095+
scope.$apply(function() {
2096+
scope.values = [{name: 'A'}];
2097+
scope.isBlank = true;
2098+
});
2099+
2100+
expect(element.find('option').length).toBe(2);
2101+
option = element.find('option').eq(0);
2102+
expect(option.val()).toBe('');
2103+
expect(option.text()).toBe('blank');
2104+
2105+
scope.$apply(function() {
2106+
scope.isBlank = false;
2107+
});
2108+
2109+
expect(element.find('option').length).toBe(1);
2110+
option = element.find('option').eq(0);
2111+
expect(option.text()).toBe('A');
2112+
});
20892113
});
20902114

20912115

0 commit comments

Comments
 (0)