From 629bdc7d42aed0b13ceac47a9927f36665e7bd93 Mon Sep 17 00:00:00 2001 From: Manuel Woelker Date: Sun, 2 Mar 2014 16:52:41 +0100 Subject: [PATCH] fix(select): reset select model if valid options changed by ng-options Previously, changing the available options so the current model value was no longer valid would show no selection in the UI The model value would still be the old value. Closes #6379 --- src/ng/directive/select.js | 5 +++++ test/ng/directive/selectSpec.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index 0b562cca686c..0135521bf033 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -575,6 +575,11 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) { while(optionGroupsCache.length > groupIndex) { optionGroupsCache.pop()[0].element.remove(); } + + if(!multiple && !selectedSet && modelValue !== null) { + // no value selected, clear model + ctrl.$setViewValue(undefined); + } } } } diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 6fcd1fe05f82..afdf91671ec1 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -1113,6 +1113,28 @@ describe('select', function() { browserTrigger(element, 'change'); expect(scope.selected).toEqual(null); }); + + it('should clear model if ng-options change to not include selected value (#6379)', function() { + createSelect({ + 'ng-model': 'selected', + 'ng-options': 'item for item in values' + }); + + scope.$apply(function() { + scope.values = ['A','B']; + scope.selected = scope.values[0]; + }); + + expect(element.val()).toEqual('0'); + + expect(scope.selected).toEqual(scope.values[0]); + + scope.$apply(function() { + scope.values = ['C','D']; + }); + expect(element.val()).toEqual('?'); + expect(scope.selected).toBeUndefined(); + }); });