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

Commit 4e9208f

Browse files
committed
fix(ngOptions): add / remove 'selected' attribute when select[multiple]
1 parent ba6f987 commit 4e9208f

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/ng/directive/ngOptions.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,19 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
510510
option.element.selected = false;
511511
});
512512

513-
if (value) {
513+
if (value && value.length) {
514514
value.forEach(function(item) {
515515
var option = options.getOptionFromViewValue(item);
516-
if (option) option.element.selected = true;
516+
if (option) {
517+
option.element.selected = true;
518+
option.element.setAttribute('selected', 'selected');
519+
}
517520
});
521+
522+
selectCtrl.unselectEmptyOption();
523+
524+
} else if (providedEmptyOption) {
525+
selectCtrl.selectEmptyOption();
518526
}
519527
};
520528

src/ng/directive/select.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ var SelectController =
7272

7373
self.unselectEmptyOption = function() {
7474
if (self.hasEmptyOption) {
75+
self.emptyOption[0].selected = false;
7576
self.emptyOption.removeAttr('selected');
7677
}
7778
};

test/ng/directive/ngOptionsSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -2841,6 +2841,33 @@ describe('ngOptions', function() {
28412841

28422842
expect(element.find('option')[0].selected).toEqual(false);
28432843
});
2844+
2845+
2846+
it('should add / remove the "selected" attribute on selected / unselected options', function() {
2847+
scope.values = [
2848+
{name:'black'},
2849+
{name:'white'},
2850+
{name:'red'}
2851+
];
2852+
scope.selected = [scope.values[2]];
2853+
2854+
createMultiSelect('<option value="">blank</option>');
2855+
scope.$apply();
2856+
expect(element.find('option')[0].value).toBe('');
2857+
2858+
expect(element.find('option')[0]).not.toBeMarkedAsSelected();
2859+
expect(element.find('option')[3]).toBeMarkedAsSelected();
2860+
2861+
scope.$apply('selected = []');
2862+
expect(element.find('option')[0]).toBeMarkedAsSelected();
2863+
2864+
scope.selected = [scope.values[1], scope.values[2]];
2865+
scope.$apply();
2866+
expect(element.find('option')[0]).not.toBeMarkedAsSelected();
2867+
expect(element.find('option')[2]).toBeMarkedAsSelected();
2868+
expect(element.find('option')[3]).toBeMarkedAsSelected();
2869+
});
2870+
28442871
});
28452872

28462873

0 commit comments

Comments
 (0)