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

fix(formController): remove scope reference when form is destroyed #9315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,13 @@ var formDirectiveFactory = function(isNgForm) {
parentFormCtrl.$$renameControl(controller, alias);
});
}
if (parentFormCtrl !== nullFormCtrl) {
formElement.on('$destroy', function() {
parentFormCtrl.$removeControl(controller);
if (alias) {
setter(scope, alias, undefined, alias);
}
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
});
}
formElement.on('$destroy', function() {
parentFormCtrl.$removeControl(controller);
if (alias) {
setter(scope, alias, undefined, alias);
}
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
});
}
};
}
Expand Down
20 changes: 20 additions & 0 deletions test/ng/directive/formSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ describe('form', function() {
expect(form.alias).toBeUndefined();
});

it('should remove scope reference when form with no parent form is removed from the DOM', function() {
var formController;
scope.ctrl = {};
doc = $compile(
'<div><form name="ctrl.myForm" ng-if="formPresent">' +
'<input name="alias" ng-model="value" />' +
'</form></div>')(scope);

scope.$digest();
expect(scope.ctrl.myForm).toBeUndefined();

scope.$apply('formPresent = true');
expect(scope.ctrl.myForm).toBeDefined();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use "toBeDefined()", get a reference to the form controller and use "toBe(theFormController)" (or whatever you want to call it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, run a digest before this and run the "undefined" assertion before this (and again after), just to be robust

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, although I think it is better like this and the test shouldn't test more things than it is actually testing.


formController = doc.find('form').controller('form');
expect(scope.ctrl.myForm).toBe(formController);

scope.$apply('formPresent = false');
expect(scope.ctrl.myForm).toBeUndefined();
});

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