question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ControlValueAccessor with default value

See original GitHub issue

Which @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:closed
  • Created 2 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewKushnircommented, Oct 27, 2021

What do you think?

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.

0reactions
angular-automatic-lock-bot[bot]commented, Nov 28, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found