FormGroupDirective
Binds an existing FormGroup to a DOM element.
参见
Register Form Group
The following example registers a
FormGroupwith 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'}); }
- }
NgModule
选择器
[formGroup]
属性
| 属性 | 说明 |
|---|---|
submitted: boolean | 只读 Reports whether the form submission has been triggered. |
directives: FormControlName[] | Tracks the list of added |
@Input('formGroup') | Tracks the |
@Output() | Emits an event when the form submission has been triggered. |
formDirective: Form | 只读 Returns this directive's instance. |
control: FormGroup | 只读 Returns the |
path: string[] | 只读 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. |
继承自 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
模板变量参考手册
| 标识符 | 用途 |
|---|---|
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.
方法
A lifecycle method called when the directive's inputs change. For internal use only. | |||
参数
返回值
|
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. | |||
参数
返回值 |
Retrieves the | |||
参数
返回值 |
Removes the | |||
参数
返回值
|
Adds a new | |||
参数
返回值
|
No-op method to remove the form group. | |||
参数
返回值
|
Retrieves the | |||
参数
返回值 |
Adds a new | |||
参数
返回值
|
No-op method to remove the form array. | |||
参数
返回值
|
Retrieves the | |||
参数
返回值 |
Sets the new value for the provided | ||||||
参数
返回值
|
Method called with the "submit" event is triggered on the form. Triggers the |
Method called when the "reset" event is triggered on the form. |
参数没有参数。 返回值
|
Resets the form to an initial value and resets its submitted status. |