Angular: No value accessor for form control with unspecified name attribute
I have a small project with Angular 9. And I have a component as below:
@Component( {
selector: 'switch-extra-field',
template: `
<div class="form-group" [formGroup]="formGroup" >
<switch [attr.title]="field.etiquette"
[attr.value]="field.valeur" [(ngModel)]="field.valeur"
[formControl]="fieldControl" [attr.id]="etiquette">
</switch>
</div>
`
} )
When I run this component I got an error No value accessor for form control with unspecified name attribute.
ERROR Error: No value accessor for form control with unspecified name attribute
at _throwError (forms.es5.js:1918)
at setUpControl (forms.es5.js:1828)
How can I resolve it?
-
M3
MSIIIXI Jun 21 2021
You can try to use
ngDefaultControl
directive to make your form has default 2way binding.<switch [(ngModel)]="name" ngDefaultControl></switch>
I hope it works for you.
-
Q-3
Quoc Tin Nguyen Jun 21 2021
I think the message error throw is very clear. you lost
name
attribute to the element has[ngModel]
. To fix this issue, you can add morename="fieldName" ngDefaultControl
to your element.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.