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

Commit 7f3f3dd

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 d077966 commit 7f3f3dd

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
@@ -626,7 +626,10 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
626626
if (emptyOption_ || unknownOption_) {
627627
while (current &&
628628
(current === emptyOption_ ||
629-
current === unknownOption_)) {
629+
current === unknownOption_ ||
630+
emptyOption_ && emptyOption_.nodeType === NODE_TYPE_COMMENT)) {
631+
// Empty options might have directives that transclude
632+
// and insert comments (e.g. ngIf)
630633
current = current.nextSibling;
631634
}
632635
}

test/ng/directive/ngOptionsSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,30 @@ describe('ngOptions', function() {
20952095
expect(element[0].selectedIndex).toEqual(0);
20962096
expect(scope.selected).toEqual([]);
20972097
});
2098+
2099+
2100+
it('should be possible to use ngIf in the blank option', function() {
2101+
var option;
2102+
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');
2103+
2104+
scope.$apply(function() {
2105+
scope.values = [{name: 'A'}];
2106+
scope.isBlank = true;
2107+
});
2108+
2109+
expect(element.find('option').length).toBe(2);
2110+
option = element.find('option').eq(0);
2111+
expect(option.val()).toBe('');
2112+
expect(option.text()).toBe('blank');
2113+
2114+
scope.$apply(function() {
2115+
scope.isBlank = false;
2116+
});
2117+
2118+
expect(element.find('option').length).toBe(1);
2119+
option = element.find('option').eq(0);
2120+
expect(option.text()).toBe('A');
2121+
});
20982122
});
20992123

21002124

0 commit comments

Comments
 (0)