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

Commit 01f50e1

Browse files
shahataIgorMinar
authored andcommitted
fix(formController): remove scope reference when form is destroyed
Closes #9315
1 parent 9f2ad53 commit 01f50e1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/ng/directive/form.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,13 @@ var formDirectiveFactory = function(isNgForm) {
495495
parentFormCtrl.$$renameControl(controller, alias);
496496
});
497497
}
498-
if (parentFormCtrl !== nullFormCtrl) {
499-
formElement.on('$destroy', function() {
500-
parentFormCtrl.$removeControl(controller);
501-
if (alias) {
502-
setter(scope, alias, undefined, alias);
503-
}
504-
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
505-
});
506-
}
498+
formElement.on('$destroy', function() {
499+
parentFormCtrl.$removeControl(controller);
500+
if (alias) {
501+
setter(scope, alias, undefined, alias);
502+
}
503+
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
504+
});
507505
}
508506
};
509507
}

test/ng/directive/formSpec.js

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

61+
it('should remove scope reference when form with no parent form is removed from the DOM', function() {
62+
var formController;
63+
scope.ctrl = {};
64+
doc = $compile(
65+
'<div><form name="ctrl.myForm" ng-if="formPresent">' +
66+
'<input name="alias" ng-model="value" />' +
67+
'</form></div>')(scope);
68+
69+
scope.$digest();
70+
expect(scope.ctrl.myForm).toBeUndefined();
71+
72+
scope.$apply('formPresent = true');
73+
expect(scope.ctrl.myForm).toBeDefined();
74+
75+
formController = doc.find('form').controller('form');
76+
expect(scope.ctrl.myForm).toBe(formController);
77+
78+
scope.$apply('formPresent = false');
79+
expect(scope.ctrl.myForm).toBeUndefined();
80+
});
6181

6282
it('should use ngForm value as form name', function() {
6383
doc = $compile(

0 commit comments

Comments
 (0)