Skip to content

Commit

Permalink
fix(ngOptions): skip comments when looking for option elements
Browse files Browse the repository at this point in the history
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 angular#12190
  • Loading branch information
Narretz committed Sep 27, 2015
1 parent d077966 commit 7f3f3dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,10 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
if (emptyOption_ || unknownOption_) {
while (current &&
(current === emptyOption_ ||
current === unknownOption_)) {
current === unknownOption_ ||
emptyOption_ && emptyOption_.nodeType === NODE_TYPE_COMMENT)) {
// Empty options might have directives that transclude
// and insert comments (e.g. ngIf)
current = current.nextSibling;
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,30 @@ describe('ngOptions', function() {
expect(element[0].selectedIndex).toEqual(0);
expect(scope.selected).toEqual([]);
});


it('should be possible to use ngIf in the blank option', function() {
var option;
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');

scope.$apply(function() {
scope.values = [{name: 'A'}];
scope.isBlank = true;
});

expect(element.find('option').length).toBe(2);
option = element.find('option').eq(0);
expect(option.val()).toBe('');
expect(option.text()).toBe('blank');

scope.$apply(function() {
scope.isBlank = false;
});

expect(element.find('option').length).toBe(1);
option = element.find('option').eq(0);
expect(option.text()).toBe('A');
});
});


Expand Down

0 comments on commit 7f3f3dd

Please sign in to comment.