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

NgModel

根据领域对象创建一个 FormControl 实例,并把它绑定到一个表单控件元素上。

Creates a FormControl instance from a domain model and binds it to a form control element.

查看"说明"...

参见

Exported from

选择器

属性

属性说明
control: FormControl只读
@Input()
name: string

跟踪绑定到指令的名称。如果存在父表单,它将使用此名称作为键名来检索此控件的值。

Tracks the name bound to the directive. If a parent form exists, it uses this name as a key to retrieve this control's value.

@Input('disabled')
isDisabled: boolean

跟踪控件是否被禁用。

Tracks whether the control is disabled.

@Input('ngModel')
model: any

跟踪与此指令绑定的值。

Tracks the value bound to this directive.

@Input('ngModelOptions')
options: { name?: string; standalone?: boolean; updateOn?: FormHooks; }

跟踪该 ngModel 实例的配置项。

Tracks the configuration options for this ngModel instance.

name:用来设置表单控件元素的 name 属性的另一种方式。参见把 ngModel 用作独立控件的那个例子

name: An alternative to setting the name attribute on the form control element. See the example for using NgModel as a standalone control.

standalone:如果为 true,则此 ngModel 不会把自己注册进它的父表单中,其行为就像没在表单中一样。默认为 false。

standalone: When set to true, the ngModel will not register itself with its parent form, and acts as if it's not in the form. Defaults to false. If no parent form exists, this option has no effect.

updateOn: 用来定义该何时更新表单控件的值和有效性。默认为 'change'。可能的取值为:'change' | 'blur' | 'submit'

updateOn: Defines the event upon which the form control value and validity update. Defaults to 'change'. Possible values: 'change' | 'blur' | 'submit'.

@Output('ngModelChange')
update: EventEmitter

更新视图模型后,ngModelChange 的事件发射器。

Event emitter for producing the ngModelChange event after the view model updates.

path: string[]只读

返回一个数组,该数组表示从顶级表单到此控件的路径。每个索引是该级别上控件的字符串名称。

Returns an array that represents the path from the top-level form to this control. Each index is the string name of the control on that level.

formDirective: any只读

此控件的顶级指令(如果存在),否则为 null。

The top-level directive for this control if present, otherwise null.

继承自 NgControl

继承自 AbstractControlDirective

模板变量参考手册

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

说明

这个 FormControl 实例将会跟踪值、用户交互和控件的验证状态,以保持视图与模型的同步。 如果用在某个父表单中,该指令还会把自己注册为这个父表单的子控件。

The FormControl instance tracks the value, user interaction, and validation status of the control and keeps the view synced with the model. If used within a parent form, the directive also registers itself with the form as a child control.

这个指令可以单独使用,也可以用作一个大表单的一部分。你所要做的一切就是用 ngModel 选择器来激活它。

This directive is used by itself or as part of a larger form. Use the ngModel selector to activate it.

它可以接受一个领域模型作为可选的 Input。如果使用 [] 语法来单向绑定到 ngModel,那么在组件类中修改领域模型将会更新视图中的值。 如果使用 [()] 语法来双向绑定到 ngModel,那么视图中值的变化会随时同步回组件类中的领域模型。

It accepts a domain model as an optional Input. If you have a one-way binding to ngModel with [] syntax, changing the domain model's value in the component class sets the value in the view. If you have a two-way binding with [()] syntax (also known as 'banana-in-a-box syntax'), the value in the UI always syncs back to the domain model in your class.

如果你希望查看与 FormControl 相关的属性(比如校验状态),你也可以使用 ngModel 作为键,把该指令导出到一个局部模板变量中(如:#myVar="ngModel")。 你也可以使用该指令的 control 属性来访问此控件,实际上你要用到的大多数属性(如 validdirty)都会委托给该控件,这样你就可以直接访问这些属性了。 你可以在 AbstractControlDirective 中直接查看这些属性的完整列表。

To inspect the properties of the associated FormControl (like thevalidity state), export the directive into a local template variable using ngModel as the key (ex: #myVar="ngModel"). You can then access the control using the directive's control property. However, the most commonly used properties (like valid and dirty) also exist on the control for direct access. See a full list of properties directly available in AbstractControlDirective.

在独立控件模式下使用 ngModel

Using ngModel on a standalone control

如果你希望查看与 FormControl 相关的属性(比如校验状态),你也可以使用 ngModel 作为键,把该指令导出到一个局部模板变量中(如:#myVar="ngModel")。 你也可以使用该指令的 control 属性来访问此控件,实际上你要用到的大多数属性(如 validdirty)都会委托给该控件,这样你就可以直接访问这些属性了。 你可以在 AbstractControlDirective 中直接查看这些属性的完整列表。

下面是一个在简单的独立控件中使用 ngModel 的例子:

The following examples show a simple standalone control using ngModel:

      
      import {Component} from '@angular/core';

@Component({
  selector: 'example-app',
  template: `
    <input [(ngModel)]="name" #ctrl="ngModel" required>

    <p>Value: {{ name }}</p>
    <p>Valid: {{ ctrl.valid }}</p>

    <button (click)="setValue()">Set value</button>
  `,
})
export class SimpleNgModelComp {
  name: string = '';

  setValue() {
    this.name = 'Nancy';
  }
}
    

当在 <form> 标签中使用 ngModel 时,你还需要提供一个 name 属性,以便该控件可以使用这个名字把自己注册到父表单中。

When using the ngModel within <form> tags, you'll also need to supply a name attribute so that the control can be registered with the parent form under that name.

在父表单的上下文中,通常不用包含单向或双向绑定,因为这个父表单将会为你同步该值。 你可以使用 ngForm 把它导出给一个模板局部变量(如 #f="ngForm"),以访问它的属性。 可以在任何需要提交表单的地方使用它。

In the context of a parent form, it's often unnecessary to include one-way or two-way binding, as the parent form syncs the value for you. You access its properties by exporting it into a local template variable using ngForm such as (#f="ngForm"). Use the variable where needed on form submission.

如果你只是要为表单设置初始值,对 ngModel 使用单向绑定就够了。在提交时,你可以使用从表单导出的值,而不必使用领域模型的值。

If you do need to populate initial values into your form, using a one-way binding for ngModel tends to be sufficient as long as you use the exported form's value rather than the domain model's value on submit.

在表单中使用 ngModel

Using ngModel within a form

下面的例子展示了如何在表单中使用 ngModel

The following example shows controls using ngModel within a form:

      
      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
  }
}
    

在表单组中使用独立 ngModel

Using a standalone ngModel within a group

下面的例子演示了如何在表单中使用独立 ngModel 控件。它控制表单的显示,但并不包含表单数据。

The following example shows you how to use a standalone ngModel control within a form. This controls the display of the form, but doesn't contain form data.

      
      <form>
  <input name="login" ngModel placeholder="Login">
  <input type="checkbox" ngModel [ngModelOptions]="{standalone: true}"> Show more options?
</form>
<!-- form value: {login: ''} -->
    

通过选项设置 ngModel 的 name 属性

Setting the ngModel name attribute through options

下面的例子展示了设置 name 属性的另一种方式。该 name 属性要和自定义表单组件一起使用,而该自定义组件的 @Input 属性 name 已用作其它用途。

The following example shows you an alternate way to set the name attribute. Here, an attribute identified as name is used within a custom form control component. To still be able to specify the NgModel's name, you must specify it using the ngModelOptions input instead.

      
      <form>
  <my-custom-form-control name="Nancy" ngModel [ngModelOptions]="{name: 'user'}">
  </my-custom-form-control>
</form>
<!-- form value: {user: ''} -->
    

方法

设置视图模型的新值并发出 ngModelChange 事件。

Sets the new value for the view model and emits an ngModelChange event.

      
      viewToModelUpdate(newValue: any): void
    
参数
newValue any

ngModelChange 发出的新值。

The new value emitted by ngModelChange.

返回值

void

继承自 NgControl

继承自 AbstractControlDirective