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

时间:2016-08-16 14:54:42

标签: angular typescript angular-material2

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

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

我的app.module.ts导入FormsModule

我还试图在我的组件中声明private recipient;

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

No value accessor for form control with name: 'recipient'

2 个答案:

答案 0 :(得分:116)

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

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

取自这篇文章中的评论:

angular2 rc.5 custom input, No value accessor for form control with unspecified name

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

现在你应该写:

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

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

答案 1 :(得分:11)

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