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

Commit 11d2242

Browse files
committed
fix(select): make ctrl.hasOption method consistent
Prior to this fix, options added to a select by ngOptions would not cause `selectCtrl.hasOption` to return `true` Closes #8761
1 parent cea23db commit 11d2242

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/ng/directive/select.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
589589
id: option.id,
590590
selected: option.selected
591591
});
592+
selectCtrl.addOption(option.label, element);
592593
if (lastElement) {
593594
lastElement.after(element);
594595
} else {
@@ -600,7 +601,9 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
600601
// remove any excessive OPTIONs in a group
601602
index++; // increment since the existingOptions[0] is parent element not OPTION
602603
while(existingOptions.length > index) {
603-
existingOptions.pop().element.remove();
604+
option = existingOptions.pop();
605+
selectCtrl.removeOption(option.label);
606+
option.element.remove();
604607
}
605608
}
606609
// remove any excessive OPTGROUPs from select

test/ng/directive/selectSpec.js

+66
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
});
@@ -454,6 +487,39 @@ describe('select', function() {
454487
expect(element).toBeValid();
455488
expect(element).toBeDirty();
456489
});
490+
491+
describe('selectController.hasOption', function() {
492+
it('should return true for options added via ngOptions', function() {
493+
scope.robots = [
494+
{key: 1, value: 'c3p0'},
495+
{key: 2, value: 'r2d2'}
496+
];
497+
scope.robot = 'r2d2';
498+
499+
compile('<select ng-model="robot" multiple ' +
500+
'ng-options="item.key as item.value for item in robots">' +
501+
'</select>');
502+
503+
var selectCtrl = element.data().$selectController;
504+
505+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
506+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
507+
508+
scope.$apply(function() {
509+
scope.robots.pop();
510+
});
511+
512+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
513+
expect(selectCtrl.hasOption('r2d2')).toBe(false);
514+
515+
scope.$apply(function() {
516+
scope.robots.push({key: 2, value: 'r2d2'});
517+
});
518+
519+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
520+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
521+
});
522+
});
457523
});
458524

459525

0 commit comments

Comments
 (0)