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

Commit 0f58334

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 bcc257b commit 0f58334

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
@@ -639,10 +639,15 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
639639
var emptyOption_ = emptyOption && emptyOption[0];
640640
var unknownOption_ = unknownOption && unknownOption[0];
641641

642+
// We cannot rely on the extracted empty option being the same as the compiled empty option,
643+
// because the compiled empty option might have been replaced by a comment because
644+
// it had an "element" transclusion directive on it (such as ngIf)
642645
if (emptyOption_ || unknownOption_) {
643646
while (current &&
644647
(current === emptyOption_ ||
645-
current === unknownOption_)) {
648+
current === unknownOption_ ||
649+
current.nodeType === NODE_TYPE_COMMENT ||
650+
current.value === '')) {
646651
current = current.nextSibling;
647652
}
648653
}

test/ng/directive/ngOptionsSpec.js

+46
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,52 @@ describe('ngOptions', function() {
21352135
});
21362136

21372137

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

0 commit comments

Comments
 (0)