Skip to content

Commit 2568786

Browse files
committed
fix(select): make ctrl.hasOption method consistent
Closes angular#8761
1 parent d8c8b2e commit 2568786

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/ng/directive/select.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
611611
id: option.id,
612612
selected: option.selected
613613
});
614+
selectCtrl.addOption(option.label, element);
614615
if (lastElement) {
615616
lastElement.after(element);
616617
} else {
@@ -622,7 +623,9 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
622623
// remove any excessive OPTIONs in a group
623624
index++; // increment since the existingOptions[0] is parent element not OPTION
624625
while(existingOptions.length > index) {
625-
existingOptions.pop().element.remove();
626+
option = existingOptions.pop();
627+
selectCtrl.removeOption(option.label);
628+
option.element.remove();
626629
}
627630
}
628631
// remove any excessive OPTGROUPs from select

test/ng/directive/selectSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,39 @@ describe('select', function() {
378378
expect(element).toEqualSelect(['? string:r2d2 ?']);
379379
expect(scope.robot).toBe('r2d2');
380380
});
381+
382+
describe('selectController.hasOption', function() {
383+
it('should return true for options added via ngOptions', function() {
384+
scope.robots = [
385+
{key: 1, value: 'c3p0'},
386+
{key: 2, value: 'r2d2'}
387+
];
388+
scope.robot = 'r2d2';
389+
390+
compile('<select ng-model="robot" ' +
391+
'ng-options="item.key as item.value for item in robots">' +
392+
'</select>');
393+
394+
var selectCtrl = element.data().$selectController;
395+
396+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
397+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
398+
399+
scope.$apply(function() {
400+
scope.robots.pop();
401+
});
402+
403+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
404+
expect(selectCtrl.hasOption('r2d2')).toBe(false);
405+
406+
scope.$apply(function() {
407+
scope.robots.push({key: 2, value: 'r2d2'});
408+
});
409+
410+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
411+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
412+
});
413+
});
381414
});
382415
});
383416
});

0 commit comments

Comments
 (0)