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

Commit 30db7f5

Browse files
committed
feat(form.FormController): add $getControls()
Fixes #14749 Closes #14517 Closes #13202
1 parent d7d64cc commit 30db7f5

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/ng/directive/form.js

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
var nullFormCtrl = {
66
$addControl: noop,
7+
$getControls: noop,
78
$$renameControl: nullFormRenameControl,
89
$removeControl: noop,
910
$setValidity: noop,
@@ -159,6 +160,28 @@ FormController.prototype = {
159160
control.$$parentForm = this;
160161
},
161162

163+
/**
164+
* @ngdoc method
165+
* @name form.FormController#$getControls
166+
* @returns {Array} the controls that are currently part of this form
167+
*
168+
* @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.
173+
*
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
179+
* you need access to the controls.
180+
*/
181+
$getControls: function() {
182+
return shallowCopy(this.$$controls);
183+
},
184+
162185
// Private API: rename a form control
163186
$$renameControl: function(control, newName) {
164187
var oldName = control.$name;

test/ng/directive/formSpec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,42 @@ describe('form', function() {
12001200
});
12011201
});
12021202

1203+
fdescribe('$getControls', function() {
1204+
it('should return a shallow copy of the form controls', function() {
1205+
doc = $compile(
1206+
'<form name="testForm">' +
1207+
'<input ng-model="named" name="foo">' +
1208+
'<div ng-form>' +
1209+
'<input ng-model="named" name="foo">' +
1210+
'</div>' +
1211+
'</form>')(scope);
1212+
1213+
scope.$digest();
1214+
1215+
var form = doc,
1216+
formCtrl = scope.testForm,
1217+
formInput = form.children('input').eq(0),
1218+
formInputCtrl = formInput.controller('ngModel'),
1219+
nestedForm = form.find('div'),
1220+
nestedFormCtrl = nestedForm.controller('form'),
1221+
nestedInput = nestedForm.children('input').eq(0),
1222+
nestedInputCtrl = nestedInput.controller('ngModel');
1223+
1224+
var controls = formCtrl.$getControls();
1225+
1226+
expect(controls).not.toBe(formCtrl.$$controls);
1227+
1228+
controls.push('something');
1229+
expect(formCtrl.$$controls).not.toContain('something');
1230+
1231+
expect(controls[0]).toBe(formInputCtrl);
1232+
expect(controls[1]).toBe(nestedFormCtrl);
1233+
1234+
var nestedControls = controls[1].$getControls();
1235+
1236+
expect(nestedControls[0]).toBe(nestedInputCtrl);
1237+
});
1238+
});
12031239

12041240
it('should rename nested form controls when interpolated name changes', function() {
12051241
scope.idA = 'A';

0 commit comments

Comments
 (0)