From 290b5049c2de4aa0d6ba8eea624bc6dce027b197 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Mon, 6 Jul 2015 20:53:24 +0200 Subject: [PATCH] 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 --- src/ng/directive/form.js | 1 + test/ng/directive/formSpec.js | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index 8914ddd2224f..b09142eb911d 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -168,6 +168,7 @@ function FormController(element, attrs, $scope, $animate, $interpolate) { }); arrayRemove(controls, control); + control.$$parentForm = nullFormCtrl; }; diff --git a/test/ng/directive/formSpec.js b/test/ng/directive/formSpec.js index 27d18f3032e0..6e25f1804b5c 100644 --- a/test/ng/directive/formSpec.js +++ b/test/ng/directive/formSpec.js @@ -58,6 +58,43 @@ describe('form', function() { expect(form.alias).toBeUndefined(); }); + + it('should ignore changes in manually removed controls', function() { + doc = $compile( + '
' + + '' + + '
')(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 = {};