Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got this error after upgrading to Angular 2 Rc.5. This is my component template:

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

My app.module.ts imports the FormsModule

I also tried to declare private recipient; in my component.

Am I missing something? Why do I get this error?

No value accessor for form control with name: 'recipient'
like image 897
TheUnreal Avatar asked Aug 16 '16 14:08

TheUnreal


2 Answers

You should add the ngDefaultControl attribute to your input like this:

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

Taken from comments in this post:

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

Note: For later versions of @angular/material:

Nowadays you should instead write:

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

See https://material.angular.io/components/input/overview

like image 87
Peter Salomonsen Avatar answered Oct 29 '22 23:10

Peter Salomonsen


Make sure you import MaterialModule as well since you are using md-input which does not belong to FormsModule

like image 15
Ophir Bushinsky Avatar answered Oct 30 '22 01:10

Ophir Bushinsky