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

Commit

Permalink
fix(ngOptions): skip comments and empty options when looking for options
Browse files Browse the repository at this point in the history
Related #12952
Closes #12190
Closes #13029
Closes #13033
  • Loading branch information
petebacondarwin authored and Narretz committed Oct 28, 2015
1 parent 964a901 commit 395f3ec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,15 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
var emptyOption_ = emptyOption && emptyOption[0];
var unknownOption_ = unknownOption && unknownOption[0];

// We cannot rely on the extracted empty option being the same as the compiled empty option,
// because the compiled empty option might have been replaced by a comment because
// it had an "element" transclusion directive on it (such as ngIf)
if (emptyOption_ || unknownOption_) {
while (current &&
(current === emptyOption_ ||
current === unknownOption_)) {
current === unknownOption_ ||
current.nodeType === NODE_TYPE_COMMENT ||
current.value === '')) {
current = current.nextSibling;
}
}
Expand Down
46 changes: 46 additions & 0 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,52 @@ describe('ngOptions', function() {
});


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');
});


it('should be possible to use ngIf in the blank option when values are available upon linking',
function() {
var options;

scope.values = [{name: 'A'}];
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');

scope.$apply('isBlank = true');

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

scope.$apply('isBlank = false');

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

it('should not throw when a directive compiles the blank option before ngOptions is linked', function() {
expect(function() {
createSelect({
Expand Down

0 comments on commit 395f3ec

Please sign in to comment.