Skip to content

Commit 5066110

Browse files
committed
fix(ngOptions): ignore comment nodes when removing 'selected' attribute
Related angular#15454
1 parent f1db7d7 commit 5066110

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/ng/directive/select.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ var SelectController =
7070
};
7171

7272
self.unselectEmptyOption = function() {
73-
if (self.emptyOption) {
73+
// Empty nodes with non-rendered ngIf can be comment nodes
74+
if (self.emptyOption && self.emptyOption[0].nodeType === NODE_TYPE_ELEMENT) {
7475
self.emptyOption.removeAttr('selected');
7576
}
7677
};

test/ng/directive/ngOptionsSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,25 @@ describe('ngOptions', function() {
25242524
}
25252525
);
25262526

2527+
2528+
it('should select the correct option after linking when the ngIf expression is initially falsy', function() {
2529+
scope.values = [
2530+
{name:'black'},
2531+
{name:'white'},
2532+
{name:'red'}
2533+
];
2534+
scope.selected = scope.values[2];
2535+
2536+
expect(function() {
2537+
createSingleSelect('<option ng-if="isBlank" value="">blank</option>');
2538+
scope.$apply();
2539+
}).not.toThrow();
2540+
2541+
expect(element.find('option')[2]).toBeMarkedAsSelected();
2542+
expect(linkLog).toEqual(['linkNgOptions']);
2543+
});
2544+
2545+
25272546
it('should not throw when a directive compiles the blank option before ngOptions is linked', function() {
25282547
expect(function() {
25292548
createSelect({

0 commit comments

Comments
 (0)