directive
Binds an existing FormGroup to a DOM element.
[formGroup]| Property | Description |
|---|---|
submitted: boolean | Read-Only Reports whether the form submission has been triggered. |
directives: FormControlName[] | Tracks the list of added |
@Input('formGroup')form: FormGroup | Tracks the |
@Output()ngSubmit: EventEmitter | Emits an event when the form submission has been triggered. |
override | Returns an array representing the path to this group. Because this directive always lives at the top level of a form, it always an empty array. |
formDirective: Form | Read-Only |
control: FormGroup | Read-Only |
path: string[] | Read-Only |
ControlContainer
AbstractControlDirective
abstract control: AbstractControl | null value: any valid: boolean | null invalid: boolean | null pending: boolean | null disabled: boolean | null enabled: boolean | null errors: ValidationErrors | null pristine: boolean | null dirty: boolean | null touched: boolean | null status: string | null untouched: boolean | null statusChanges: Observable<any> | null valueChanges: Observable<any> | null path: string[] | null validator: ValidatorFn | null asyncValidator: AsyncValidatorFn | null | Identifier | Usage |
|---|---|
ngForm | #myTemplateVar="ngForm" |
This directive accepts an existing FormGroup instance. It will then use this FormGroup instance to match any child FormControl, FormGroup, and FormArray instances to child FormControlName, FormGroupName, and FormArrayName directives.
The following example registers a FormGroup with first name and last name controls, and listens for the ngSubmit event when the button is clicked.
import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div *ngIf="first.invalid"> Name is too short. </div>
<input formControlName="first" placeholder="First name">
<input formControlName="last" placeholder="Last name">
<button type="submit">Submit</button>
</form>
<button (click)="setValue()">Set preset value</button>
`,
})
export class SimpleFormGroup {
form = new FormGroup({
first: new FormControl('Nancy', Validators.minLength(2)),
last: new FormControl('Drew'),
});
get first(): any {
return this.form.get('first');
}
onSubmit(): void {
console.log(this.form.value); // {first: 'Nancy', last: 'Drew'}
}
setValue() {
this.form.setValue({first: 'Carson', last: 'Drew'});
}
} | addControl() | |||
|---|---|---|---|
| Method that sets up the control directive in this group, re-calculates its value and validity, and adds the instance to the internal list of directives. | |||
|
dir | FormControlName | The |
| getControl() | |||
|---|---|---|---|
| Retrieves the | |||
|
dir | FormControlName | The |
| removeControl() | |||
|---|---|---|---|
| Removes the | |||
|
dir | FormControlName | The |
void
| addFormGroup() | |||
|---|---|---|---|
| Adds a new | |||
|
dir | FormGroupName | The |
void
| removeFormGroup() | |||
|---|---|---|---|
| Performs the necessary cleanup when a | |||
|
dir | FormGroupName | The |
void
| getFormGroup() | |||
|---|---|---|---|
| Retrieves the | |||
|
dir | FormGroupName | The |
| addFormArray() | |||
|---|---|---|---|
| Performs the necessary setup when a | |||
|
dir | FormArrayName | The |
void
| removeFormArray() | |||
|---|---|---|---|
| Performs the necessary cleanup when a | |||
|
dir | FormArrayName | The |
void
| getFormArray() | |||
|---|---|---|---|
| Retrieves the | |||
|
dir | FormArrayName | The |
| updateModel() | ||||||
|---|---|---|---|---|---|---|
| Sets the new value for the provided | ||||||
|
dir | FormControlName | The |
value | any | The new value for the directive's control. |
void
| onSubmit() |
|---|
| Method called with the "submit" event is triggered on the form. Triggers the |
| onReset() |
|---|
| Method called when the "reset" event is triggered on the form. |
|
| resetForm() | |||
|---|---|---|---|
| Resets the form to an initial value and resets its submitted status. | |||
|
value | any | The new value for the form. Optional. Default is |
void
AbstractControlDirective
© 2010–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v12.angular.io/api/forms/FormGroupDirective