Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 - No value accessor for form control with unspecified name attribute

I'm getting this error on my Angular 7 app and I don't know why. Even with some researches it seems that this error doesn't make sense.

This is what I have:

<mat-form-field>
     <input matInput placeholder="Name" name="name" [(ngModel)]="data.name" [disabled]="inputReadonly" ngDefaultControl > 
</mat-form-field>

I've tried with and withoud ngDefaultControl on mat-form-field and/or input, with and without the name attribute and to move [(ngModel)] on input. Nothing is changed, I'm still receiving the same error.

like image 295
DeooK Avatar asked Dec 17 '18 10:12

DeooK


People also ask

What is Ng_value_accessor?

NG_VALUE_ACCESSORlinkUsed to provide a ControlValueAccessor for form controls.

What is form control name in angular?

FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name.

What is ControlValueAccessor in angular?

Defines an interface that acts as a bridge between the Angular forms API and a native element in the DOM.


2 Answers

ngModel has to be specified on the input, not on the mat-form-field. The only purpose of mat-form-field is to apply some Material CSS

like image 92
Guerric P Avatar answered Oct 23 '22 03:10

Guerric P


make sure you have MatFormFieldModule and MatInputModule imported in your app.module.ts and also declared in @NgModule({... imports:[MatFormFieldModule,MatInputModule, ...],...}

like image 3
viks Avatar answered Oct 23 '22 03:10

viks