NgForm

创建一个顶级的 FormGroup 实例,并把它绑定到一个表单,以跟踪表单的聚合值及其验证状态。

Creates a top-level FormGroup instance and binds it to a form to track aggregate form value and validation status.

查看"说明"...

NgModule

选择器

属性

属性说明
submitted: boolean 只读

Returns whether the form submission has been triggered.

form: FormGroup

The FormGroup instance created for this form.

@Output()
ngSubmit: EventEmitter

Event emitter for the "ngSubmit" event

@Input('ngFormOptions')
options: { updateOn?: FormHooks; }

NgForm 实例的选项。接受下列属性:

Tracks options for the NgForm instance.

updateOn:为所有子级的 NgModel 设置 updateOn 的默认值(除非子 NgModel 通过 ngModelOptions 显式指定了这个值)。 可能的值有:'change' | 'blur' | 'submit'.

updateOn: Sets the default updateOn value for all child NgModels below it unless explicitly set by a child NgModel using ngModelOptions). Defaults to 'change'. Possible values: 'change' | 'blur' | 'submit'

formDirective: Form 只读

The directive instance.

control: FormGroup 只读

The internal FormGroup instance.

path: string[] 只读

Returns an array representing the path to this group. Because this directive always lives at the top level of a form, it is always an empty array.

controls: { [key: string]: AbstractControl; } 只读

Returns a map of the controls in this group.

继承自 ControlContainer

继承自 AbstractControlDirective

模板变量参考手册

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

说明

只要你导入了 FormsModule,该指令就会默认在所有 <form> 标签上生效。你不需要再添加任何特殊的选择器。

As soon as you import the FormsModule, this directive becomes active by default on all <form> tags. You don't need to add a special selector.

你可以以 ngForm 作为 key 把该指令导出到一个局部模板变量(如 #myForm="ngForm")。这是可选的,但很有用。 来自本指令背后的 FormGroup 实例的很多属性,都被复制到了指令自身,所以拿到一个对该指令的引用就可以让你访问此表单的聚合值和验证状态, 还有那些用户交互类的属性,比如 dirtytouched

You optionally export the directive into a local template variable using ngForm as the key (ex: #myForm="ngForm"). This is optional, but useful. Many properties from the underlying FormGroup instance are duplicated on the directive itself, so a reference to it gives you access to the aggregate value and validity status of the form, as well as user interaction properties like dirty and touched.

To register child controls with the form, use NgModel with a name attribute. You may use NgModelGroup to create sub-groups within the form.

如果需要,还可以监听该指令的 ngSubmit 事件,以便当用户触发了一次表单提交时得到通知。发出 ngSubmit 事件时,会携带原始的 DOM 表单提交事件。

If necessary, listen to the directive's ngSubmit event to be notified when the user has triggered a form submission. The ngSubmit event emits the original form submission event.

在模板驱动表单中,所有 <form> 标签都会自动应用上 NgForm 指令。 如果你只想导入 FormsModule 而不想把它应用于某些表单中,比如,要想使用 HTML5 验证,你可以添加 ngNoForm 属性, 这样标签就不会在 <form> 上创建 NgForm 指令了。 在响应式表单中,则不需要用 ngNoForm,因为 NgForm 指令不会自动应用到 <form> 标签上,你只要别主动添加 formGroup 指令就可以了。

In template driven forms, all <form> tags are automatically tagged as NgForm. To import the FormsModule but skip its usage in some forms, for example, to use native HTML5 validation, add the ngNoForm and the <form> tags won't create an NgForm directive. In reactive forms, using ngNoForm is unnecessary because the <form> tags are inert. In that case, you would refrain from using the formGroup directive.

从废弃的 ngForm 选择器迁移过来

Migrating from deprecated ngForm selector

ngForm 元素选择器的支持已经在 Angular v6 中废弃,并将在 Angular v9 中移除。

Support for using ngForm element selector has been deprecated in Angular v6 and will be removed in Angular v9.

之所以弃用它,是我们要让所有选择器都跟其它核心 Angular 选择器保持统一,而元素选择器通常写作中线格式。

This has been deprecated to keep selectors consistent with other core Angular selectors, as element selectors are typically written in kebab-case.

已废弃的写法:

Now deprecated:

<ngForm #myForm="ngForm">
      
      <ngForm #myForm="ngForm">
    

以后的写法:

After:

<ng-form #myForm="ngForm">
      
      <ng-form #myForm="ngForm">
    

Listening for form submission

The following example shows how to capture the form values from the "ngSubmit" event.

import {Component} from '@angular/core'; import {NgForm} from '@angular/forms'; @Component({ selector: 'example-app', template: ` <form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate> <input name="first" ngModel required #first="ngModel"> <input name="last" ngModel> <button>Submit</button> </form> <p>First name value: {{ first.value }}</p> <p>First name valid: {{ first.valid }}</p> <p>Form value: {{ f.value | json }}</p> <p>Form valid: {{ f.valid }}</p> `, }) export class SimpleFormComp { onSubmit(f: NgForm) { console.log(f.value); // { first: '', last: '' } console.log(f.valid); // false } }
      
      
  1. import {Component} from '@angular/core';
  2. import {NgForm} from '@angular/forms';
  3.  
  4. @Component({
  5. selector: 'example-app',
  6. template: `
  7. <form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
  8. <input name="first" ngModel required #first="ngModel">
  9. <input name="last" ngModel>
  10. <button>Submit</button>
  11. </form>
  12. <p>First name value: {{ first.value }}</p>
  13. <p>First name valid: {{ first.valid }}</p>
  14. <p>Form value: {{ f.value | json }}</p>
  15. <p>Form valid: {{ f.valid }}</p>
  16. `,
  17. })
  18. export class SimpleFormComp {
  19. onSubmit(f: NgForm) {
  20. console.log(f.value); // { first: '', last: '' }
  21. console.log(f.valid); // false
  22. }
  23. }

Setting the update options

The following example shows you how to change the "updateOn" option from its default using ngFormOptions.

<form [ngFormOptions]="{updateOn: 'blur'}"> <input name="one" ngModel> <!-- this ngModel will update on blur --> </form>
      
      <form [ngFormOptions]="{updateOn: 'blur'}">
   <input name="one" ngModel>  <!-- this ngModel will update on blur -->
</form>
    

方法

Lifecycle method called after the view is initialized. For internal use only.

ngAfterViewInit()
      
      ngAfterViewInit()
    
参数

没有参数。

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: NgModel): void
      
      addControl(dir: NgModel): void
    
参数
dir NgModel

The NgModel directive instance.

返回值

void

Retrieves the FormControl instance from the provided NgModel directive.

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

The NgModel directive instance.

返回值

FormControl

Removes the NgModel instance from the internal list of directives

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

The NgModel directive instance.

返回值

void

Adds a new NgModelGroup directive instance to the form.

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

The NgModelGroup directive instance.

返回值

void

Removes the NgModelGroup directive instance from the form.

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

The NgModelGroup directive instance.

返回值

void

Retrieves the FormGroup for a provided NgModelGroup directive instance

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

The NgModelGroup directive instance.

返回值

FormGroup

Sets the new value for the provided NgControl directive.

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

The NgControl directive instance.

value any

The new value for the directive's control.

返回值

void

Sets the value for this FormGroup.

setValue(value: { [key: string]: any; }): void
      
      setValue(value: { [key: string]: any; }): void
    
参数
value object

The new value

返回值

void

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

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

The "submit" event object

返回值

boolean

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

onReset(): void
      
      onReset(): void
    
参数

没有参数。

返回值

void

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

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

可选. 默认值是 undefined.

The new value for the form.

返回值

void

继承自 AbstractControlDirective