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

fix(select): allow ngOptions to be set in $timeout #10203

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
@@ -209,10 +209,10 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
};


self.removeOption = function(value) {
self.removeOption = function(value, anySelected) {
if (this.hasOption(value)) {
delete optionsMap[value];
if (ngModelCtrl.$viewValue == value) {
if (ngModelCtrl.$viewValue == value && !anySelected) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to explain why it is necessary to check !anySelected here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the expression on line 215 evaluates to true, then we execute the statement:

this.renderUnknownOption(value);

But, we should only render the unknown option if no option is currently selected. If any option is selected, the select element should only show the values defined in ngOptions.

this.renderUnknownOption(value);
}
}
@@ -683,7 +683,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
if (count > 0) {
selectCtrl.addOption(label);
} else if (count < 0) {
selectCtrl.removeOption(label);
selectCtrl.removeOption(label, anySelected);
}
});
}
24 changes: 24 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
@@ -233,6 +233,30 @@ describe('select', function() {
expect(scope.robot).toBe('');
});

it('should not add the empty string when an option is selected and options are set in a timeout', inject(function($timeout) {

compile('<select ng-model="simpleModel" ng-options="opt.id as opt.label for opt in simpleOpts">' +
'</select>');

scope.$apply(function() {
scope.simpleModel = 0;
});

$timeout(function() {
scope.simpleOpts = [
{id: 0, label: 'x'},
{id: 1, label: 'y'}
];
}, 0);

$timeout.flush();

var options = element.find('option');

expect(options.length).toEqual(2);
expect(options.eq(0)).toEqualOption('0', 'x');
expect(options.eq(1)).toEqualOption('1', 'y');
}));

describe('interactions with repeated options', function() {