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

Commit

Permalink
fix(ngModel): do not throw if view value changes on destroyed scope
Browse files Browse the repository at this point in the history
This could for example happen if updating the value is debounced (either
by asynchronously calling `$setViewValue()` or via `ngModelOptions`).

Fixes #16583

Closes #16585
  • Loading branch information
gkalpak committed May 31, 2018
1 parent 2da4950 commit 2b6c986
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ function NgModelController($scope, $exceptionHandler, $attr, $element, $parse, $
this.$$currentValidationRunId = 0;

this.$$scope = $scope;
this.$$rootScope = $scope.$root;
this.$$attr = $attr;
this.$$element = $element;
this.$$animate = $animate;
Expand Down Expand Up @@ -864,7 +865,7 @@ NgModelController.prototype = {
this.$$pendingDebounce = this.$$timeout(function() {
that.$commitViewValue();
}, debounceDelay);
} else if (this.$$scope.$root.$$phase) {
} else if (this.$$rootScope.$$phase) {
this.$commitViewValue();
} else {
this.$$scope.$apply(function() {
Expand Down
10 changes: 8 additions & 2 deletions test/ng/directive/ngModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('ngModel', function() {

describe('NgModelController', function() {
/* global NgModelController: false */
var ctrl, scope, ngModelAccessor, element, parentFormCtrl;
var ctrl, scope, element, parentFormCtrl;

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

scope = $rootScope;
ngModelAccessor = jasmine.createSpy('ngModel accessor');
ctrl = $controller(NgModelController, {
$scope: scope,
$element: element.find('input'),
Expand Down Expand Up @@ -438,6 +437,13 @@ describe('ngModel', function() {
expect(ctrl.$modelValue).toBe('c');
expect(scope.value).toBe('c');
}));


it('should not throw an error if the scope has been destroyed', function() {
scope.$destroy();
ctrl.$setViewValue('some-val');
expect(ctrl.$viewValue).toBe('some-val');
});
});


Expand Down

0 comments on commit 2b6c986

Please sign in to comment.