填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

FormGroupDirective

将现有的 FormGroup 绑定到 DOM 元素。

Binds an existing FormGroup to a DOM element.

查看"说明"...

参见

Exported from

选择器

  • [formGroup]

属性

属性说明
submitted: boolean只读

报告是否已触发表单提交。

Reports whether the form submission has been triggered.

directives: FormControlName[]

跟踪已添加的 FormControlName 实例的列表

Tracks the list of added FormControlName instances

@Input('formGroup')
form: FormGroup

跟踪绑定到此指令的 FormGroup

Tracks the FormGroup bound to this directive.

@Output()
ngSubmit: EventEmitter

触发表单提交后,发出事件。

Emits an event when the form submission has been triggered.

formDirective: Form只读

返回此指令的实例。

Returns this directive's instance.

control: FormGroup只读

返回绑定到该指令的 FormGroup

Returns the FormGroup bound to this directive.

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

模板变量参考手册

标识符用途
ngForm#myTemplateVar="ngForm"

说明

该指令接受现有的 FormGroup 实例。然后,它将使用此 FormGroup 实例中的任何子控件 FormControlFormGroupFormArray 的实例与其子指令 FormControlNameFormGroupNameFormArrayName 匹配。

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.

注册表单组

Register Form Group

下面的示例使用名字和姓氏控件 FormGroup ,并在单击按钮时侦听 ngSubmit 事件。

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'});
  }
}
    

方法

在该组中设置控件指令,重新计算其值和有效性并将该实例添加到内部指令列表的方法。

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.

      
      addControl(dir: FormControlName): FormControl
    
参数
dir FormControlName

FormControlName 指令实例。

The FormControlName directive instance.

返回值

FormControl

从给定 FormControlName 指令中检索 FormControl

Retrieves the FormControl instance from the provided FormControlName directive

      
      getControl(dir: FormControlName): FormControl
    
参数
dir FormControlName

FormControlName 指令实例。

The FormControlName directive instance.

返回值

FormControl

从内部指令列表中删除此 FormControlName 实例

Removes the FormControlName instance from the internal list of directives

      
      removeControl(dir: FormControlName): void
    
参数
dir FormControlName

FormControlName 指令实例。

The FormControlName directive instance.

返回值

void

将新的 FormGroupName 指令实例添加到表单。

Adds a new FormGroupName directive instance to the form.

      
      addFormGroup(dir: FormGroupName): void
    
参数
dir FormGroupName

FormGroupName 指令实例。

The FormGroupName directive instance.

返回值

void

用于删除表单组的空白方法。

Performs the necessary cleanup when a FormGroupName directive instance is removed from the view.

      
      removeFormGroup(dir: FormGroupName): void
    
参数
dir FormGroupName

FormGroupName 指令实例。

The FormGroupName directive instance.

返回值

void

检索给定 FormGroupName 指令实例的 FormGroup

Retrieves the FormGroup for a provided FormGroupName directive instance

      
      getFormGroup(dir: FormGroupName): FormGroup
    
参数
dir FormGroupName

FormGroupName 指令实例。

The FormGroupName directive instance.

返回值

FormGroup

向表单添加一个新的 FormArrayName 指令实例。

Performs the necessary setup when a FormArrayName directive instance is added to the view.

      
      addFormArray(dir: FormArrayName): void
    
参数
dir FormArrayName

FormArrayName 指令实例。

The FormArrayName directive instance.

返回值

void

用于删除表单组的空白方法。

Performs the necessary cleanup when a FormArrayName directive instance is removed from the view.

      
      removeFormArray(dir: FormArrayName): void
    
参数
dir FormArrayName

FormArrayName 指令实例。

The FormArrayName directive instance.

返回值

void

检索给定 FormArrayName 指令实例的 FormArray

Retrieves the FormArray for a provided FormArrayName directive instance.

      
      getFormArray(dir: FormArrayName): FormArray
    
参数
dir FormArrayName

FormArrayName 指令实例。

The FormArrayName directive instance.

返回值

FormArray

为给定 FormControlName 指令设置新值。

Sets the new value for the provided FormControlName directive.

      
      updateModel(dir: FormControlName, value: any): void
    
参数
dir FormControlName

FormControlName 指令实例。

The FormControlName directive instance.

value any

指令控件的新值。

The new value for the directive's control.

返回值

void

当表单上触发了 “submit” 事件时要调用的方法。触发带有 “submit” 事件的 ngSubmit

Method called with the "submit" event is triggered on the form. Triggers the ngSubmit emitter to emit the "submit" event as its payload.

      
      onSubmit($event: Event): boolean
    
参数
$event Event

"submit" 事件对象

The "submit" event object

返回值

boolean

在表单上触发“reset”事件时调用的方法。

Method called when the "reset" event is triggered on the form.

      
      onReset(): void
    
参数

没有参数。

返回值

void

将表单重置为初始值并重置其提交状态。

Resets the form to an initial value and resets its submitted status.

      
      resetForm(value: any = undefined): void
    
参数
value any

表单的新值。

The new value for the form.

可选. 默认值是 `undefined`.
返回值

void

继承自 AbstractControlDirective