From 9ac5f160d7800b37a6b02da07dda4a0f7d8a9ea7 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Tue, 25 Nov 2014 22:09:59 +0100 Subject: [PATCH] fix(select): use strict compare when removing option from ctrl Otherwise, if the removed option was the empty option (value ''), and the currently selected option had a value of 0, the select would think that the currently selected option had been removed, causing the unknown option to be added again. Fixes #9714 Fixes #10115 Closes #10203 --- src/ng/directive/select.js | 2 +- test/ng/directive/selectSpec.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index ed372c26e31d..a7433fb85f2f 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -212,7 +212,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) { self.removeOption = function(value) { if (this.hasOption(value)) { delete optionsMap[value]; - if (ngModelCtrl.$viewValue == value) { + if (ngModelCtrl.$viewValue === value) { this.renderUnknownOption(value); } } diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 772fb79f8f3d..2a624130fc69 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -250,6 +250,31 @@ describe('select', function() { expect(scope.robot).toBe(''); }); + it('should not be set when an option is selected and options are set asynchronously', + inject(function($timeout) { + compile(''); + + scope.$apply(function() { + scope.model = 0; + }); + + $timeout(function() { + scope.options = [ + {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() {