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

Commit b6d0284

Browse files
committed
fixup! feat(form.FormController): add $getControls()
1 parent 30db7f5 commit b6d0284

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/ng/directive/form.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
var nullFormCtrl = {
66
$addControl: noop,
7-
$getControls: noop,
7+
$getControls: valueFn([]),
88
$$renameControl: nullFormRenameControl,
99
$removeControl: noop,
1010
$setValidity: noop,
@@ -166,16 +166,18 @@ FormController.prototype = {
166166
* @returns {Array} the controls that are currently part of this form
167167
*
168168
* @description
169-
* This method returns a **shallow copy** of the controls that are currently part of this form
170-
* ({@link form.FormController `FormController`} /
171-
* {@link ngModel.NgModelController `NgModelController`}) . This can be used
172-
* for example to iterate over all controls to validate them.
169+
* This method returns a **shallow copy** of the controls that are currently part of this form.
170+
* The controls can be instances of {@link form.FormController `FormController`}
171+
* ({@link ngForm "child-forms"}) and of {@link ngModel.NgModelController `NgModelController`}.
172+
* If you need access to the controls of sub-forms, you have to call `$getControls()`
173+
* recursively on them.
174+
* This can be used for example to iterate over all controls to validate them.
173175
*
174-
* The controls can be accessed normally, but adding or removing controls from the array has no
175-
* effect on the form. Instead, use {@link form.FormController#$addControl `$addControl()`} and
176-
* {@link form.FormController#$removeControl `$removeControl()`}.
177-
* Likewise, adding a control to / removing a control from the form is not reflected
178-
* in the shallow copy. That means you should get a fresh copy from `$getControls` every time
176+
* The controls can be accessed normally, but adding to, or removing controls from the array has
177+
* no effect on the form. Instead, use {@link form.FormController#$addControl `$addControl()`} and
178+
* {@link form.FormController#$removeControl `$removeControl()`} for this use-case.
179+
* Likewise, adding a control to, or removing a control from the form is not reflected
180+
* in the shallow copy. That means you should get a fresh copy from `$getControls()` every time
179181
* you need access to the controls.
180182
*/
181183
$getControls: function() {

test/ng/directive/formSpec.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,18 @@ describe('form', function() {
12001200
});
12011201
});
12021202

1203-
fdescribe('$getControls', function() {
1203+
describe('$getControls', function() {
1204+
it('should return an empty array if the controller has no controls', function() {
1205+
doc = $compile('<form name="testForm"></form>')(scope);
1206+
1207+
scope.$digest();
1208+
1209+
var form = doc,
1210+
formCtrl = scope.testForm;
1211+
1212+
expect(formCtrl.$getControls()).toEqual([]);
1213+
});
1214+
12041215
it('should return a shallow copy of the form controls', function() {
12051216
doc = $compile(
12061217
'<form name="testForm">' +

0 commit comments

Comments
 (0)