Skip to content

Commit 62f242c

Browse files
committed
fix(select): allow ngOptions to be set in $timeout
Fix angular 1.3 regression where empty string was shown Closes angular#9714
1 parent 42d09f1 commit 62f242c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/ng/directive/select.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
209209
};
210210

211211

212-
self.removeOption = function(value) {
212+
self.removeOption = function(value, anySelected) {
213213
if (this.hasOption(value)) {
214214
delete optionsMap[value];
215-
if (ngModelCtrl.$viewValue == value) {
215+
if (ngModelCtrl.$viewValue == value && !anySelected) {
216216
this.renderUnknownOption(value);
217217
}
218218
}
@@ -683,7 +683,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
683683
if (count > 0) {
684684
selectCtrl.addOption(label);
685685
} else if (count < 0) {
686-
selectCtrl.removeOption(label);
686+
selectCtrl.removeOption(label, anySelected);
687687
}
688688
});
689689
}

test/ng/directive/selectSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,30 @@ describe('select', function() {
233233
expect(scope.robot).toBe('');
234234
});
235235

236+
it('should not add the empty string when an option is selected and options are set in a timeout', inject(function($timeout) {
237+
238+
compile('<select ng-model="simpleModel" ng-options="opt.id as opt.label for opt in simpleOpts">' +
239+
'</select>');
240+
241+
scope.$apply(function() {
242+
scope.simpleModel = 0;
243+
});
244+
245+
$timeout(function() {
246+
scope.simpleOpts = [
247+
{id: 0, label: 'x'},
248+
{id: 1, label: 'y'}
249+
];
250+
}, 0);
251+
252+
$timeout.flush();
253+
254+
var options = element.find('option');
255+
256+
expect(options.length).toEqual(2);
257+
expect(options.eq(0)).toEqualOption('0', 'x');
258+
expect(options.eq(1)).toEqualOption('1', 'y');
259+
}));
236260

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

0 commit comments

Comments
 (0)