表单控件的值访问器没有名称:'recipient'

The*_*eal 65 typescript angular-material2 angular

升级到Angular 2 Rc.5后出现此错误.这是我的组件模板:

<md-input
    [(ngModel)]="recipient"
    name="recipient"
    placeholder="Name"
    class="col-sm-4"
    (blur)="addRecipient(recipient)">
</md-input>
Run Code Online (Sandbox Code Playgroud)

我的app.module.ts导入了 FormsModule

我也尝试private recipient;在我的组件中声明.

我错过了什么吗?为什么我会收到此错误?

No value accessor for form control with name: 'recipient'
Run Code Online (Sandbox Code Playgroud)

Pet*_*sen 125

您应该将ngDefaultControl属性添加到输入中,如下所示:

<md-input
    [(ngModel)]="recipient"
    name="recipient"
    placeholder="Name"
    class="col-sm-4"
    (blur)="addRecipient(recipient)"
    ngDefaultControl>
</md-input>
Run Code Online (Sandbox Code Playgroud)

摘自这篇文章中的评论:

angular2 rc.5自定义输入,没有用于具有未指定名称的表单控件的值访问器

注意:对于@ angular/material的更高版本:

现在你应该写:

<md-input-container>
    <input
        mdInput
        [(ngModel)]="recipient"
        name="recipient"
        placeholder="Name"
        (blur)="addRecipient(recipient)">
</md-input-container>
Run Code Online (Sandbox Code Playgroud)

请参阅https://material.angular.io/components/input/overview

  • 它适用于我,但有人可以解释我为什么要添加"ngDefaultControl"? (3认同)

Oph*_*sky 11

确保您导入MaterialModule,因为您使用的是不属于FormsModule的md-input