From 50755af1e699c48a19ecbf020ed5175c2c47c21e Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Sat, 3 Dec 2016 22:48:24 +0200 Subject: [PATCH 1/2] fix(select): do not throw when removing the element (e.g. via `ngIf`) Fixes #15466 --- src/ng/directive/select.js | 4 ++++ test/ng/directive/selectSpec.js | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index 20271e2077aa..a0842c055d09 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -76,9 +76,11 @@ var SelectController = } }; + var scopeDestroyed = false; $scope.$on('$destroy', function() { // disable unknown option so that we don't do work when the whole select is being destroyed self.renderUnknownOption = noop; + scopeDestroyed = true; }); // Read the value of the select control, the implementation of this changes depending @@ -182,6 +184,8 @@ var SelectController = updateScheduled = true; $scope.$$postDigest(function() { + if (scopeDestroyed) return; + updateScheduled = false; self.ngModelCtrl.$setViewValue(self.readValue()); if (renderAfter) self.ngModelCtrl.$render(); diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 51b093728fd6..6990cfeb9eb9 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -7,7 +7,7 @@ describe('select', function() { formElement = jqLite('
' + html + '
'); element = formElement.find('select'); $compile(formElement)(scope); - scope.$apply(); + scope.$digest(); } function compileRepeatedOptions() { @@ -767,6 +767,20 @@ describe('select', function() { expect(element).toEqualSelect([unknownValue()], '1', '2', '3'); } ); + + + it('should not throw when removing the element and all its children', function() { + var template = + ''; + scope.visible = true; + + compile(template); + + // It should not throw when removing the element + scope.$apply('visible = false'); + }); }); From 7dfa950513d5b9b80f81cac6f47c13cec67d4521 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Tue, 6 Dec 2016 11:42:05 +0200 Subject: [PATCH 2/2] fixup! fix(select): do not throw when removing the element (e.g. via `ngIf`) --- src/ng/directive/select.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index a0842c055d09..55381ff72835 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -76,11 +76,9 @@ var SelectController = } }; - var scopeDestroyed = false; $scope.$on('$destroy', function() { // disable unknown option so that we don't do work when the whole select is being destroyed self.renderUnknownOption = noop; - scopeDestroyed = true; }); // Read the value of the select control, the implementation of this changes depending @@ -184,7 +182,7 @@ var SelectController = updateScheduled = true; $scope.$$postDigest(function() { - if (scopeDestroyed) return; + if ($scope.$$destroyed) return; updateScheduled = false; self.ngModelCtrl.$setViewValue(self.readValue());