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

Commit 86618c2

Browse files
committed
fix(form): allow dynamic form names which initially evaluate to blank
1 parent 5b52286 commit 86618c2

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/ng/directive/form.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,12 @@ var formDirectiveFactory = function(isNgForm) {
454454
name: 'form',
455455
restrict: isNgForm ? 'EAC' : 'E',
456456
controller: FormController,
457-
compile: function ngFormCompile(formElement) {
457+
compile: function ngFormCompile(formElement, attr) {
458458
// Setup initial state of the control
459459
formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
460460

461+
var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
462+
461463
return {
462464
pre: function ngFormPreLink(scope, formElement, attr, controller) {
463465
// if `action` attr is not present on the form, prevent the default action (submission)
@@ -488,23 +490,21 @@ var formDirectiveFactory = function(isNgForm) {
488490
});
489491
}
490492

491-
var parentFormCtrl = controller.$$parentForm,
492-
alias = controller.$name;
493-
494-
if (alias) {
495-
setter(scope, alias, controller, alias);
496-
attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) {
497-
if (alias === newValue) return;
498-
setter(scope, alias, undefined, alias);
499-
alias = newValue;
500-
setter(scope, alias, controller, alias);
501-
parentFormCtrl.$$renameControl(controller, alias);
493+
var parentFormCtrl = controller.$$parentForm;
494+
495+
if (nameAttr) {
496+
setter(scope, controller.$name, controller, controller.$name);
497+
attr.$observe(nameAttr, function(newValue) {
498+
if (controller.$name === newValue) return;
499+
setter(scope, controller.$name, undefined, controller.$name);
500+
parentFormCtrl.$$renameControl(controller, newValue);
501+
setter(scope, controller.$name, controller, controller.$name);
502502
});
503503
}
504504
formElement.on('$destroy', function() {
505505
parentFormCtrl.$removeControl(controller);
506-
if (alias) {
507-
setter(scope, alias, undefined, alias);
506+
if (nameAttr) {
507+
setter(scope, attr[nameAttr], undefined, controller.$name);
508508
}
509509
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
510510
});

test/ng/directive/formSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,16 @@ describe('form', function() {
895895
expect(form2.$name).toBe('nameB');
896896
});
897897

898+
it('should rename forms with an initially blank name', function() {
899+
var element = $compile('<form name="{{name}}"></form>')(scope);
900+
scope.$digest();
901+
var form = element.controller('form');
902+
expect(form.$name).toBe('');
903+
scope.name = 'foo';
904+
scope.$digest();
905+
expect(form.$name).toBe('foo');
906+
expect(scope.foo).toBe(form);
907+
});
898908

899909
describe('$setSubmitted', function() {
900910
beforeEach(function() {

0 commit comments

Comments
 (0)