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

Commit 290b504

Browse files
committed
fix(ngModel): remove reference to parentForm from removed control
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
1 parent f8a07dd commit 290b504

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/ng/directive/form.js

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ function FormController(element, attrs, $scope, $animate, $interpolate) {
168168
});
169169

170170
arrayRemove(controls, control);
171+
control.$$parentForm = nullFormCtrl;
171172
};
172173

173174

test/ng/directive/formSpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,43 @@ describe('form', function() {
5858
expect(form.alias).toBeUndefined();
5959
});
6060

61+
62+
it('should ignore changes in manually removed controls', function() {
63+
doc = $compile(
64+
'<form name="myForm">' +
65+
'<input name="control" ng-maxlength="1" ng-model="value" store-model-ctrl/>' +
66+
'</form>')(scope);
67+
68+
var form = scope.myForm;
69+
70+
var input = doc.find('input').eq(0);
71+
var inputController = input.controller('ngModel');
72+
73+
changeInputValue(input, 'ab');
74+
scope.$apply();
75+
76+
expect(form.$error.maxlength).toBeTruthy();
77+
expect(form.$dirty).toBe(true);
78+
expect(form.$error.maxlength[0].$name).toBe('control');
79+
80+
// remove control
81+
form.$removeControl(form.control);
82+
expect(form.control).toBeUndefined();
83+
expect(form.$error.maxlength).toBeFalsy();
84+
85+
inputController.$setPristine();
86+
expect(form.$dirty).toBe(true);
87+
88+
form.$setPristine();
89+
90+
changeInputValue(input, 'abc');
91+
scope.$apply();
92+
93+
expect(form.$error.maxlength).toBeFalsy();
94+
expect(form.$dirty).toBe(false);
95+
});
96+
97+
6198
it('should remove scope reference when form with no parent form is removed from the DOM', function() {
6299
var formController;
63100
scope.ctrl = {};

0 commit comments

Comments
 (0)