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

Commit 2bcd02d

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 b0033a4 commit 2bcd02d

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
@@ -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

+66
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,39 @@ describe('select', function() {
405405
expect(element).toEqualSelect(['? string:r2d2 ?']);
406406
expect(scope.robot).toBe('r2d2');
407407
});
408+
409+
describe('selectController.hasOption', function() {
410+
it('should return true for options added via ngOptions', function() {
411+
scope.robots = [
412+
{key: 1, value: 'c3p0'},
413+
{key: 2, value: 'r2d2'}
414+
];
415+
scope.robot = 'r2d2';
416+
417+
compile('<select ng-model="robot" ' +
418+
'ng-options="item.key as item.value for item in robots">' +
419+
'</select>');
420+
421+
var selectCtrl = element.data().$selectController;
422+
423+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
424+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
425+
426+
scope.$apply(function() {
427+
scope.robots.pop();
428+
});
429+
430+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
431+
expect(selectCtrl.hasOption('r2d2')).toBe(false);
432+
433+
scope.$apply(function() {
434+
scope.robots.push({key: 2, value: 'r2d2'});
435+
});
436+
437+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
438+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
439+
});
440+
});
408441
});
409442
});
410443
});
@@ -481,6 +514,39 @@ describe('select', function() {
481514
expect(element).toBeValid();
482515
expect(element).toBeDirty();
483516
});
517+
518+
describe('selectController.hasOption', function() {
519+
it('should return true for options added via ngOptions', function() {
520+
scope.robots = [
521+
{key: 1, value: 'c3p0'},
522+
{key: 2, value: 'r2d2'}
523+
];
524+
scope.robot = 'r2d2';
525+
526+
compile('<select ng-model="robot" multiple ' +
527+
'ng-options="item.key as item.value for item in robots">' +
528+
'</select>');
529+
530+
var selectCtrl = element.data().$selectController;
531+
532+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
533+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
534+
535+
scope.$apply(function() {
536+
scope.robots.pop();
537+
});
538+
539+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
540+
expect(selectCtrl.hasOption('r2d2')).toBe(false);
541+
542+
scope.$apply(function() {
543+
scope.robots.push({key: 2, value: 'r2d2'});
544+
});
545+
546+
expect(selectCtrl.hasOption('c3p0')).toBe(true);
547+
expect(selectCtrl.hasOption('r2d2')).toBe(true);
548+
});
549+
});
484550
});
485551

486552

0 commit comments

Comments
 (0)