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

Commit 459997b

Browse files
committed
fix(ngModel): do not throw if view value changes on destroyed scope
This could for example happen if updating the value is debounced (either by asynchronously calling `$setViewValue()` or via `ngModelOptions`). Fixes #16583 Closes #16585
1 parent 8e104ee commit 459997b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/ng/directive/ngModel.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ function NgModelController($scope, $exceptionHandler, $attr, $element, $parse, $
287287
this.$$currentValidationRunId = 0;
288288

289289
this.$$scope = $scope;
290+
this.$$rootScope = $scope.$root;
290291
this.$$attr = $attr;
291292
this.$$element = $element;
292293
this.$$animate = $animate;
@@ -864,7 +865,7 @@ NgModelController.prototype = {
864865
this.$$pendingDebounce = this.$$timeout(function() {
865866
that.$commitViewValue();
866867
}, debounceDelay);
867-
} else if (this.$$scope.$root.$$phase) {
868+
} else if (this.$$rootScope.$$phase) {
868869
this.$commitViewValue();
869870
} else {
870871
this.$$scope.$apply(function() {

test/ng/directive/ngModelSpec.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('ngModel', function() {
66

77
describe('NgModelController', function() {
88
/* global NgModelController: false */
9-
var ctrl, scope, ngModelAccessor, element, parentFormCtrl;
9+
var ctrl, scope, element, parentFormCtrl;
1010

1111
beforeEach(inject(function($rootScope, $controller) {
1212
var attrs = {name: 'testAlias', ngModel: 'value'};
@@ -21,7 +21,6 @@ describe('ngModel', function() {
2121
element = jqLite('<form><input></form>');
2222

2323
scope = $rootScope;
24-
ngModelAccessor = jasmine.createSpy('ngModel accessor');
2524
ctrl = $controller(NgModelController, {
2625
$scope: scope,
2726
$element: element.find('input'),
@@ -438,6 +437,13 @@ describe('ngModel', function() {
438437
expect(ctrl.$modelValue).toBe('c');
439438
expect(scope.value).toBe('c');
440439
}));
440+
441+
442+
it('should not throw an error if the scope has been destroyed', function() {
443+
scope.$destroy();
444+
ctrl.$setViewValue('some-val');
445+
expect(ctrl.$viewValue).toBe('some-val');
446+
});
441447
});
442448

443449

0 commit comments

Comments
 (0)