Skip to content

Commit a7b8624

Browse files
committed
docs(forms): Documentation and examples for Control, ControlGroup, and ControlArray.
Closes angular#4398
1 parent 7ae74cf commit a7b8624

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

modules/angular2/src/core/forms/model.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,20 @@ export class AbstractControl {
132132
}
133133

134134
/**
135-
* Defines a part of a form that cannot be divided into other controls.
135+
* Defines a part of a form that cannot be divided into other controls. `Control`s have values and
136+
* validation state, which is determined by an optional validation function.
136137
*
137138
* `Control` is one of the three fundamental building blocks used to define forms in Angular, along
138139
* with {@link ControlGroup} and {@link ControlArray}.
140+
*
141+
* # Usage
142+
*
143+
* By default, a `Control` is created for every `<input>` or other form component.
144+
* With {@link NgFormControl} or {@link NgFormModel} an existing {@link Control} can be
145+
* bound to a DOM element instead. This `Control` can be configured with a custom
146+
* validation function.
147+
*
148+
* ### Example ([live demo](http://plnkr.co/edit/23DESOpbNnBpBHZt1BR4?p=preview))
139149
*/
140150
export class Control extends AbstractControl {
141151
_onChange: Function;
@@ -147,6 +157,18 @@ export class Control extends AbstractControl {
147157
this._valueChanges = new EventEmitter();
148158
}
149159

160+
/**
161+
* Set the value of the control to `value`.
162+
*
163+
* If `onlySelf` is `true`, this change will only affect the validation of this `Control`
164+
* and not its parent component. If `emitEvent` is `true`, this change will cause a
165+
* `valueChanges` event on the `Control` to be emitted. Both of these options default to
166+
* `false`.
167+
*
168+
* If `emitModelToViewChange` is `true`, the view will be notified about the new value
169+
* via an `onChange` event. This is the default behavior if `emitModelToViewChange` is not
170+
* specified.
171+
*/
150172
updateValue(value: any,
151173
{onlySelf, emitEvent, emitModelToViewChange}:
152174
{onlySelf?: boolean, emitEvent?: boolean, emitModelToViewChange?: boolean} = {}):
@@ -157,6 +179,9 @@ export class Control extends AbstractControl {
157179
this.updateValueAndValidity({onlySelf: onlySelf, emitEvent: emitEvent});
158180
}
159181

182+
/**
183+
* Register a listener for change events.
184+
*/
160185
registerOnChange(fn: Function): void { this._onChange = fn; }
161186
}
162187

@@ -170,6 +195,8 @@ export class Control extends AbstractControl {
170195
* `ControlGroup` is one of the three fundamental building blocks used to define forms in Angular,
171196
* along with {@link Control} and {@link ControlArray}. {@link ControlArray} can also contain other
172197
* controls, but is of variable length.
198+
*
199+
* ### Example ([live demo](http://plnkr.co/edit/23DESOpbNnBpBHZt1BR4?p=preview))
173200
*/
174201
export class ControlGroup extends AbstractControl {
175202
private _optionals: StringMap<string, boolean>;
@@ -247,6 +274,16 @@ export class ControlGroup extends AbstractControl {
247274
* `ControlArray` is one of the three fundamental building blocks used to define forms in Angular,
248275
* along with {@link Control} and {@link ControlGroup}. {@link ControlGroup} can also contain
249276
* other controls, but is of fixed length.
277+
*
278+
* # Adding or removing controls
279+
*
280+
* To change the controls in the array, use the `push`, `insert`, or `removeAt` methods
281+
* in `ControlArray` itself. These methods ensure the controls are properly tracked in the
282+
* form's hierarchy. Do not modify the array of `AbstractControl`s used to instantiate
283+
* the `ControlArray` directly, as that will result in strange and unexpected behavior such
284+
* as broken change detection.
285+
*
286+
* ### Example ([live demo](http://plnkr.co/edit/23DESOpbNnBpBHZt1BR4?p=preview))
250287
*/
251288
export class ControlArray extends AbstractControl {
252289
constructor(public controls: AbstractControl[], validator: Function = Validators.array) {
@@ -259,25 +296,40 @@ export class ControlArray extends AbstractControl {
259296
this.updateValidity({onlySelf: true});
260297
}
261298

299+
/**
300+
* Get the {@link AbstractControl} at the given `index` in the array.
301+
*/
262302
at(index: number): AbstractControl { return this.controls[index]; }
263303

304+
/**
305+
* Insert a new {@link AbstractControl} at the end of the array.
306+
*/
264307
push(control: AbstractControl): void {
265308
this.controls.push(control);
266309
control.setParent(this);
267310
this.updateValueAndValidity();
268311
}
269312

313+
/**
314+
* Insert a new {@link AbstractControl} at the given `index` in the array.
315+
*/
270316
insert(index: number, control: AbstractControl): void {
271317
ListWrapper.insert(this.controls, index, control);
272318
control.setParent(this);
273319
this.updateValueAndValidity();
274320
}
275321

322+
/**
323+
* Remove the control at the given `index` in the array.
324+
*/
276325
removeAt(index: number): void {
277326
ListWrapper.removeAt(this.controls, index);
278327
this.updateValueAndValidity();
279328
}
280329

330+
/**
331+
* Get the length of the control array.
332+
*/
281333
get length(): number { return this.controls.length; }
282334

283335
_updateValue(): void { this._value = this.controls.map((control) => control.value); }

0 commit comments

Comments
 (0)