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

Commit f68232b

Browse files
committed
fix(select): do not break when removing the element (e.g. via ngIf)
Fixes #15466
1 parent 752b1e6 commit f68232b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/ng/directive/select.js

+4
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ var SelectController =
7676
}
7777
};
7878

79+
var scopeDestroyed = false;
7980
$scope.$on('$destroy', function() {
8081
// disable unknown option so that we don't do work when the whole select is being destroyed
8182
self.renderUnknownOption = noop;
83+
scopeDestroyed = true;
8284
});
8385

8486
// Read the value of the select control, the implementation of this changes depending
@@ -182,6 +184,8 @@ var SelectController =
182184
updateScheduled = true;
183185

184186
$scope.$$postDigest(function() {
187+
if (scopeDestroyed) return;
188+
185189
updateScheduled = false;
186190
self.ngModelCtrl.$setViewValue(self.readValue());
187191
if (renderAfter) self.ngModelCtrl.$render();

test/ng/directive/selectSpec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('select', function() {
77
formElement = jqLite('<form name="form">' + html + '</form>');
88
element = formElement.find('select');
99
$compile(formElement)(scope);
10-
scope.$apply();
10+
scope.$digest();
1111
}
1212

1313
function compileRepeatedOptions() {
@@ -767,6 +767,20 @@ describe('select', function() {
767767
expect(element).toEqualSelect([unknownValue()], '1', '2', '3');
768768
}
769769
);
770+
771+
772+
it('should not break when removing the element and all its children', function() {
773+
var template =
774+
'<select ng-model="mySelect" ng-if="visible">' +
775+
'<option value="">--- Select ---</option>' +
776+
'</select>';
777+
scope.visible = true;
778+
779+
compile(template);
780+
781+
// It should not throw when removing the element
782+
scope.$apply('visible = false');
783+
});
770784
});
771785

772786

0 commit comments

Comments
 (0)