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

Commit 823adb2

Browse files
committed
fix(ngForm): alias name||ngForm
form directive was requiring name attribute even when invoked as attribute, resulting in unnecessary duplication
1 parent 21e74c2 commit 823adb2

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

Diff for: src/directive/form.js

+39-21
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ var nullFormCtrl = {
3131
* of `FormController`.
3232
*
3333
*/
34-
FormController.$inject = ['name', '$element', '$attrs'];
35-
function FormController(name, element, attrs) {
34+
FormController.$inject = ['$element', '$attrs'];
35+
function FormController(element, attrs) {
3636
var form = this,
37-
parentForm = element.parent().inheritedData('$formController') || nullFormCtrl,
37+
parentForm = element.parent().controller('form') || nullFormCtrl,
3838
invalidCount = 0, // used to easily determine if we are valid
3939
errors = form.$error = {};
4040

@@ -45,9 +45,6 @@ function FormController(name, element, attrs) {
4545
form.$valid = true;
4646
form.$invalid = false;
4747

48-
// publish the form into scope
49-
name(this);
50-
5148
parentForm.$addControl(form);
5249

5350
// Setup initial state of the control
@@ -130,9 +127,24 @@ function FormController(name, element, attrs) {
130127

131128

132129
/**
130+
* @ngdoc directive
131+
* @name angular.module.ng.$compileProvider.directive.ng-form
132+
* @restrict EAC
133+
*
134+
* @description
135+
* Nestable alias of {@link angular.module.ng.$compileProvider.directive.form `form`} directive. HTML
136+
* does not allow nesting of form elements. It is useful to nest forms, for example if the validity of a
137+
* sub-group of controls needs to be determined.
138+
*
139+
* @param {string=} ng-form|name Name of the form. If specified, the form controller will be published into
140+
* related scope, under this name.
141+
*
142+
*/
143+
144+
/**
133145
* @ngdoc directive
134146
* @name angular.module.ng.$compileProvider.directive.form
135-
* @restrict EA
147+
* @restrict E
136148
*
137149
* @description
138150
* Directive that instantiates
@@ -141,12 +153,12 @@ function FormController(name, element, attrs) {
141153
* If `name` attribute is specified, the form controller is published onto the current scope under
142154
* this name.
143155
*
144-
* # Alias: `ng-form`
156+
* # Alias: {@link angular.module.ng.$compileProvider.directive.ng-form `ng-form`}
145157
*
146158
* In angular forms can be nested. This means that the outer form is valid when all of the child
147159
* forms are valid as well. However browsers do not allow nesting of `<form>` elements, for this
148-
* reason angular provides `<ng-form>` alias which behaves identical to `<form>` but allows
149-
* element nesting.
160+
* reason angular provides {@link angular.module.ng.$compileProvider.directive.ng-form `ng-form`} alias
161+
* which behaves identical to `<form>` but allows form nesting.
150162
*
151163
*
152164
* # CSS classes
@@ -218,25 +230,31 @@ function FormController(name, element, attrs) {
218230
</doc:scenario>
219231
</doc:example>
220232
*/
221-
var formDirectiveDev = {
233+
var formDirectiveDir = {
222234
name: 'form',
223235
restrict: 'E',
224-
inject: {
225-
name: 'accessor'
226-
},
227236
controller: FormController,
228237
compile: function() {
229238
return {
230239
pre: function(scope, formElement, attr, controller) {
231-
formElement.bind('submit', function(event) {
232-
if (!attr.action) event.preventDefault();
233-
});
240+
if (!attr.action) {
241+
formElement.bind('submit', function(event) {
242+
event.preventDefault();
243+
});
244+
}
245+
246+
var parentFormCtrl = formElement.parent().controller('form'),
247+
alias = attr.name || attr.ngForm;
234248

235-
var parentFormCtrl = formElement.parent().inheritedData('$formController');
249+
if (alias) {
250+
scope[alias] = controller;
251+
}
236252
if (parentFormCtrl) {
237253
formElement.bind('$destroy', function() {
238254
parentFormCtrl.$removeControl(controller);
239-
if (attr.name) delete scope[attr.name];
255+
if (alias) {
256+
scope[alias] = undefined;
257+
}
240258
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
241259
});
242260
}
@@ -245,5 +263,5 @@ var formDirectiveDev = {
245263
}
246264
};
247265

248-
var formDirective = valueFn(formDirectiveDev);
249-
var ngFormDirective = valueFn(extend(copy(formDirectiveDev), {restrict:'EAC'}));
266+
var formDirective = valueFn(formDirectiveDir);
267+
var ngFormDirective = valueFn(extend(copy(formDirectiveDir), {restrict: 'EAC'}));

Diff for: test/directive/formSpec.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ describe('form', function() {
3333

3434
it('should remove the widget when element removed', function() {
3535
doc = $compile(
36-
'<form name="myForm">' +
37-
'<input type="text" name="alias" ng-model="value" store-model-ctrl/>' +
38-
'</form>')(scope);
36+
'<form name="myForm">' +
37+
'<input type="text" name="alias" ng-model="value" store-model-ctrl/>' +
38+
'</form>')(scope);
3939

4040
var form = scope.myForm;
4141
control.$setValidity('required', false);
@@ -48,6 +48,17 @@ describe('form', function() {
4848
});
4949

5050

51+
it('should use ng-form as form name', function() {
52+
doc = $compile(
53+
'<div ng-form="myForm">' +
54+
'<input type="text" name="alias" ng-model="value"/>' +
55+
'</div>')(scope);
56+
57+
expect(scope.myForm).toBeDefined();
58+
expect(scope.myForm.alias).toBeDefined();
59+
});
60+
61+
5162
it('should prevent form submission', function() {
5263
var startingUrl = '' + window.location;
5364
doc = jqLite('<form name="myForm"><input type="submit" value="submit" />');
@@ -89,8 +100,7 @@ describe('form', function() {
89100
it('should allow form name to be an expression', function() {
90101
doc = $compile('<form name="obj.myForm"></form>')(scope);
91102

92-
expect(scope.obj).toBeDefined();
93-
expect(scope.obj.myForm).toBeTruthy();
103+
expect(scope['obj.myForm']).toBeTruthy();
94104
});
95105

96106

0 commit comments

Comments
 (0)