ERROR Error: No value accessor for form control with unspecified name attribute on switch
I fixed this error by adding the name="fieldName" ngDefaultControl
attributes to the element that carries the [(ngModel)]
attribute.
I had the same problem and the issue was that my child component had an @input
named formControl
.
So I just needed to change from:
<my-component [formControl]="formControl"><my-component/>
to:
<my-component [control]="control"><my-component/>
ts:
@Input()
control:FormControl;
I also received this error while writing a custom form control component in Angular 7. However, none of the answers are applicable to Angular 7.
In my case, the following needed to be add to the @Component
decorator:
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyCustomComponent), // replace name as appropriate
multi: true
}
]
This is a case of "I don't know why it works, but it does." Chalk it up to poor design/implementation on the part of Angular.