Skip to content

Commit 11d2b93

Browse files
committed
fix(ngOptions): ignore comment nodes when removing 'selected' attribute
Closes angular#15454
1 parent b748046 commit 11d2b93

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ng/directive/ngOptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
452452
var removeEmptyOption = function() {
453453
if (!providedEmptyOption) {
454454
emptyOption.remove();
455-
} else {
455+
} else if (emptyOption[0].nodeType === NODE_TYPE_ELEMENT) {
456456
emptyOption.removeAttr('selected');
457457
}
458458
};

test/ng/directive/ngOptionsSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,22 @@ describe('ngOptions', function() {
25572557
expect(linkLog).toEqual(['linkCompileContents', 'linkNgOptions']);
25582558
});
25592559

2560+
it('should select the correct option after linking when the ngIf expression is initially falsy', function() {
2561+
scope.values = [
2562+
{name:'black'},
2563+
{name:'white'},
2564+
{name:'red'}
2565+
];
2566+
scope.selected = scope.values[2];
2567+
2568+
expect(function() {
2569+
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');
2570+
scope.$apply();
2571+
}).not.toThrow();
2572+
2573+
expect(element.find('option')[2]).toBeMarkedAsSelected();
2574+
expect(linkLog).toEqual(['linkNgOptions']);
2575+
});
25602576

25612577
it('should not throw with a directive that replaces', inject(function($templateCache, $httpBackend) {
25622578
$templateCache.put('select_template.html', '<select ng-options="option as option for option in selectable_options"> <option value="">This is a test</option> </select>');

0 commit comments

Comments
 (0)