patternjavascriptangularCritical
Angular4 - No value accessor for form control
Viewed 0 times
formangular4accessorforcontrolvalue
Problem
I have a custom element :
When I try to add the formControlName, I get an error message:
ERROR Error: No value accessor for form control with name:
'surveyType'
I tried to add
It seems it's because there is no input/select... and I dont know what to do.
I would like to bind my click to this formControl in order that when someone clicks on the entire card that would push my 'type' into the formControl. Is it possible?
{{ type.icon }}
{{ type.description }}
When I try to add the formControlName, I get an error message:
ERROR Error: No value accessor for form control with name:
'surveyType'
I tried to add
ngDefaultControl without success.It seems it's because there is no input/select... and I dont know what to do.
I would like to bind my click to this formControl in order that when someone clicks on the entire card that would push my 'type' into the formControl. Is it possible?
Solution
You can use
Implement the interface
So, in order to do what you want, you have to create a component which implements
Register a provider
Then, you have to tell Angular that this directive is a
The provider should provide
For example, if your custom directive is named MyControlComponent, you should add something along the following lines inside the object passed to
Usage
Your component is ready to be used. With template-driven forms,
With reactive forms, you can now properly use
Resources
formControlName only on directives which implement ControlValueAccessor.Implement the interface
So, in order to do what you want, you have to create a component which implements
ControlValueAccessor, which means implementing the following three functions:writeValue(tells Angular how to write value from model into view)
registerOnChange(registers a handler function that is called when the view changes)
registerOnTouched(registers a handler to be called when the component receives a touch event, useful for knowing if the component has been focused).
Register a provider
Then, you have to tell Angular that this directive is a
ControlValueAccessor (interface is not gonna cut it since it is stripped from the code when TypeScript is compiled to JavaScript). You do this by registering a provider. The provider should provide
NG_VALUE_ACCESSOR and use an existing value. You'll also need a forwardRef here. Note that NG_VALUE_ACCESSOR should be a multi provider.For example, if your custom directive is named MyControlComponent, you should add something along the following lines inside the object passed to
@Component decorator:providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => MyControlComponent),
}
]
Usage
Your component is ready to be used. With template-driven forms,
ngModel binding will now work properly.With reactive forms, you can now properly use
formControlName and the form control will behave as expected.Resources
- Custom Form Controls in Angular by Thoughtram
- Angular Custom Form Controls with Reactive Forms and NgModel by Cory Rylan
Context
Stack Overflow Q#45659742, score: 332
Revisions (0)
No revisions yet.