ControlValueAccessor with default value
See original GitHub issueWhich @angular/* package(s) are relevant/releated to the feature request?
forms
Description
It would be nice if it was possible to create a ControlValueAccessor that is able to set default values on its own.
For the moment this is not possible, as calling the onChange function (function that we got a handle to via registerOnChange) will make the outer form dirty.
Proposed solution
Maybe add an extra method to the ControlValueAccessor interface for retrieving the initial value (“getInitialValue()”)?
Alternatives considered
As said, I tried to propagate the initial value in the registerOnChange method:
form = new FormGroup({
name: 'initial value'
})
registerOnChange(fn: (_: any) => void): void {
this.onChange = fn;
// propagate initial value
fn(this.form.value);
}
But this makes the outer form dirty, so it’s not really an option.
So for the moment the only possibility is that all users of the ControlValueAccessor has to set the initial value themselfves
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
DefaultValueAccessor - Angular
The default ControlValueAccessor for writing a value and listening to changes on input elements. The accessor is used by the FormControlDirective ...
Read more >Angular 2 can't set default value for ControlValueAccessor ...
I've implemented ControlValueAccessor for a custom select, but I can't get it to set a default value when the component is first initialised ......
Read more >ControlValueAccessor in Angular - Medium
Implement this interface to create a custom form control directive that integrates with Angular forms.
Read more >Using Angular ControlValueAccessor interface
Using Angular ControlValueAccessor interface. This topic will go over how to use Smart CheckBox component and use ngModel forms and Reactive Forms.
Read more >Creating custom form controls using Control Value Accessor ...
Custom Form Controls using ControlValueAccessor. For all the default form controls like input field, radio button, checkbox, select dropdown ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It sounds like it should be possible to handle in your custom ControlValueAccessor by adding extra logic (extra flags?). If there is a particular problem with the NgModel directive that you are observing (unrelated to your changes in the ControlValueAccessor), please create a new ticket with a minimal repro, so we can investigate further.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.