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

Commit 4407e81

Browse files
damrbabybtford
authored andcommitted
feat(ngForm): Supports expression in form names
<form name="ctrl.form"> form controller will accessible as $scope.ctrl.form instead of $scope['ctrl.form'] BREAKING CHANGE: If you have form names that will evaluate as an expression: <form name="ctrl.form"> And if you are accessing the form from your controller: Before: function($scope) { $scope['ctrl.form'] // form controller instance } After: function($scope) { $scope.ctrl.form // form controller instance } This makes it possible to access a form from a controller using the new "controller as" syntax. Supporting the previous behavior offers no benefit.
1 parent ad76e77 commit 4407e81

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/ng/directive/form.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,13 @@ var formDirectiveFactory = function(isNgForm) {
306306
alias = attr.name || attr.ngForm;
307307

308308
if (alias) {
309-
scope[alias] = controller;
309+
setter(scope, alias, controller, alias);
310310
}
311311
if (parentFormCtrl) {
312312
formElement.bind('$destroy', function() {
313313
parentFormCtrl.$removeControl(controller);
314314
if (alias) {
315-
scope[alias] = undefined;
315+
setter(scope, alias, undefined, alias);
316316
}
317317
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
318318
});

test/ng/directive/formSpec.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ describe('form', function() {
8383
});
8484

8585

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

89-
expect(scope['obj.myForm']).toBeTruthy();
89+
expect(scope.obj).toBeDefined();
90+
expect(scope.obj.myForm).toBeTruthy();
9091
});
9192

9293

@@ -325,6 +326,30 @@ describe('form', function() {
325326
});
326327

327328

329+
it('should deregister a child form whose name is an expression when its DOM is removed', function() {
330+
doc = jqLite(
331+
'<form name="parent">' +
332+
'<div class="ng-form" name="child.form">' +
333+
'<input ng:model="modelA" name="inputA" required>' +
334+
'</div>' +
335+
'</form>');
336+
$compile(doc)(scope);
337+
scope.$apply();
338+
339+
var parent = scope.parent,
340+
child = scope.child.form;
341+
342+
expect(parent).toBeDefined();
343+
expect(child).toBeDefined();
344+
expect(parent.$error.required).toEqual([child]);
345+
doc.children().remove(); //remove child
346+
347+
expect(parent.child).toBeUndefined();
348+
expect(scope.child.form).toBeUndefined();
349+
expect(parent.$error.required).toBe(false);
350+
});
351+
352+
328353
it('should deregister a input when its removed from DOM', function() {
329354
doc = jqLite(
330355
'<form name="parent">' +

0 commit comments

Comments
 (0)