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

Commit 8ea802a

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 be62193 commit 8ea802a

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
@@ -335,13 +335,13 @@ var formDirectiveFactory = function(isNgForm) {
335335
alias = attr.name || attr.ngForm;
336336

337337
if (alias) {
338-
scope[alias] = controller;
338+
setter(scope, alias, controller, alias);
339339
}
340340
if (parentFormCtrl) {
341341
formElement.on('$destroy', function() {
342342
parentFormCtrl.$removeControl(controller);
343343
if (alias) {
344-
scope[alias] = undefined;
344+
setter(scope, alias, undefined, alias);
345345
}
346346
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
347347
});

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)