Skip to content

Commit 426d80b

Browse files
committed
fix(select): do not break when removing the element (e.g. via ngIf)
Fixes angular#15466
1 parent b77defd commit 426d80b

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
@@ -75,9 +75,11 @@ var SelectController =
7575
}
7676
};
7777

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

8385
// Read the value of the select control, the implementation of this changes depending
@@ -179,6 +181,8 @@ var SelectController =
179181
updateScheduled = true;
180182

181183
$scope.$$postDigest(function() {
184+
if (scopeDestroyed) return;
185+
182186
updateScheduled = false;
183187
self.ngModelCtrl.$setViewValue(self.readValue());
184188
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)