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

Commit

Permalink
fix(ngModel): remove reference to parentForm from removed control
Browse files Browse the repository at this point in the history
This fixes cases where the control gets removed, but the control's
element stays in the DOM.
Previously, if the removed control's validity changed, a reference to
it would again be stored on its former parent form.

Fixes #12263
  • Loading branch information
Narretz committed Sep 4, 2015
1 parent f8a07dd commit 290b504
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ng/directive/form.js
Expand Up @@ -168,6 +168,7 @@ function FormController(element, attrs, $scope, $animate, $interpolate) {
});

arrayRemove(controls, control);
control.$$parentForm = nullFormCtrl;
};


Expand Down
37 changes: 37 additions & 0 deletions test/ng/directive/formSpec.js
Expand Up @@ -58,6 +58,43 @@ describe('form', function() {
expect(form.alias).toBeUndefined();
});


it('should ignore changes in manually removed controls', function() {
doc = $compile(
'<form name="myForm">' +
'<input name="control" ng-maxlength="1" ng-model="value" store-model-ctrl/>' +
'</form>')(scope);

var form = scope.myForm;

var input = doc.find('input').eq(0);
var inputController = input.controller('ngModel');

changeInputValue(input, 'ab');
scope.$apply();

expect(form.$error.maxlength).toBeTruthy();
expect(form.$dirty).toBe(true);
expect(form.$error.maxlength[0].$name).toBe('control');

// remove control
form.$removeControl(form.control);
expect(form.control).toBeUndefined();
expect(form.$error.maxlength).toBeFalsy();

inputController.$setPristine();
expect(form.$dirty).toBe(true);

form.$setPristine();

changeInputValue(input, 'abc');
scope.$apply();

expect(form.$error.maxlength).toBeFalsy();
expect(form.$dirty).toBe(false);
});


it('should remove scope reference when form with no parent form is removed from the DOM', function() {
var formController;
scope.ctrl = {};
Expand Down

0 comments on commit 290b504

Please sign in to comment.