ERROR Error: No value accessor for form control with unspecified name attribute on switch
Solution 1:
I fixed this error by adding the name="fieldName" ngDefaultControl
attributes to the element that carries the [(ngModel)]
attribute.
Solution 2:
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;
Solution 3:
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.
Solution 4:
I also had the same error , angular 7
<button (click)="Addcity(city.name)" [(ngModel)]="city.name" class="dropdown-item fontstyle"
*ngFor="let city of Cities; let i = index">
{{city.name}}
</button>
I just added ngDefaultControl
<button (click)="Addcity(city.name)" [(ngModel)]="city.name" ngDefaultControl class="dropdown-item fontstyle"
*ngFor="let city of Cities; let i = index">
{{city.name}}