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

feat(ngForm): Supports expressions in form name #3115

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
6 changes: 3 additions & 3 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function FormController(element, attrs) {
</doc:example>
*/
var formDirectiveFactory = function(isNgForm) {
return ['$timeout', function($timeout) {
return ['$timeout', '$parse', function($timeout, $parse) {
var formDirective = {
name: 'form',
restrict: 'E',
Expand Down Expand Up @@ -335,13 +335,13 @@ var formDirectiveFactory = function(isNgForm) {
alias = attr.name || attr.ngForm;

if (alias) {
scope[alias] = controller;
$parse(alias).assign(scope, controller);
}
if (parentFormCtrl) {
formElement.on('$destroy', function() {
parentFormCtrl.$removeControl(controller);
if (alias) {
scope[alias] = undefined;
$parse(alias).assign(scope, undefined);
}
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
});
Expand Down
29 changes: 27 additions & 2 deletions test/ng/directive/formSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ describe('form', function() {
});


it('should allow form name to be an expression', function() {
it('should support expression in form name', function() {
doc = $compile('<form name="obj.myForm"></form>')(scope);

expect(scope['obj.myForm']).toBeTruthy();
expect(scope.obj).toBeDefined();
expect(scope.obj.myForm).toBeTruthy();
});


Expand Down Expand Up @@ -314,6 +315,30 @@ describe('form', function() {
});


it('should deregister a child form whose name is an expression when its DOM is removed', function() {
doc = jqLite(
'<form name="parent">' +
'<div class="ng-form" name="child.form">' +
'<input ng:model="modelA" name="inputA" required>' +
'</div>' +
'</form>');
$compile(doc)(scope);
scope.$apply();

var parent = scope.parent,
child = scope.child.form;

expect(parent).toBeDefined();
expect(child).toBeDefined();
expect(parent.$error.required).toEqual([child]);
doc.children().remove(); //remove child

expect(parent.child).toBeUndefined();
expect(scope.child.form).toBeUndefined();
expect(parent.$error.required).toBe(false);
});


it('should deregister a input when its removed from DOM', function() {
doc = jqLite(
'<form name="parent">' +
Expand Down