New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueAccessor.writeValue is being called twice, first time with a phantom null value #14988
Comments
|
I don't understand how this relates to synchronous or asynchronous. ngModel is bound to a property declared as |
|
@IgorMinar Thanks Igor. When do you think it could be fixed? |
because |
|
Yes, and this check will break initialization in reactive forms scenario, where this "phantom" null is not passed. |
|
how it will break initialization in reactive forms scenario? |
|
If initial value of a bound property is null, the |
|
Ok I see how to fix it but they don't merge fixes until v4 release :) |
|
@DzmitryShylovich Why do you think it can't fixed in v2? |
|
it will be fixed in 2.x but the fix will be available after v4 release |
|
I see, thanks for the clarification. It's better then nothing anyway:) |
|
It is the same in Angular 4.1.0: |
|
What is the status on this ? |
|
This is an open issue for us too. @DzmitryShylovich, any status on this? |
|
Any news on this issue? I'm creating custom component and for my case it is very important to do some logic on first writeValue call, but since only second writeValue call is giving real value this is a big issue :) |
|
Problem still exists in Angular 5.2.1. Any news on that? Is this by design? |
|
Still present in latest angular version. |
|
This is still an issue. I have a nested user control Form component : PartsList Component :
`
Part Component has [(ngModel)]="part.quantity" When writeValue is first called with null, I can put a safety band aid around it to check for null inside the part component.But the template engine screws up the [(ngModel)]= "part.quantity" and complains about "Cannot read property 'quantity' of undefined". |
|
This bug is ruining my marriage. Please fix. I know. I'm not worthy. |
|
@ckelley7 You can check for null value http://plnkr.co/edit/UNYbyNK609F9o2vOtzRy?p=preview Until it's fixed. At lest you won't get exception. |
|
@RoyiNamir : Thanks for the suggestion, but this won't help the errors encountered if you reference your innerValue in your template. My component template has The template engine still tries to navigate to innerValue.quantity with this error |
|
@SV-efi Have you tried wrapping it with a Form tag? as this will cause initialization to be schedule : problematic : https://plnkr.co/edit/fOL57Hjw2PAOx6WXh36t?p=preview Solution : https://plnkr.co/edit/5Ez9VgN0yNz7QaF7DMck?p=preview |
`ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if an optional flag is given. Fixes angular#14988
`ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if the value is a special sentinel value indicating it has not yet been initialized. Fixes angular#14988
`ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if the value is a special sentinel value indicating it has not yet been initialized. Fixes angular#14988
`ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if the value is a special sentinel value indicating it has not yet been initialized. Fixes angular#14988
|
Until the bug fix is merged for people who still have this problem and the problem for calling |
`ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if an optional flag is given. Fixes angular#14988 fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null revert: "fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null" This reverts commit 0babb27. fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null fix(forms): ValueAccessor.writeValue(...) sometimes called with null `ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if the value is a special sentinel value indicating it has not yet been initialized. Fixes angular#14988 fix(forms): ValueAccessor.writeValue(...) sometimes called with null `ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if the value is a special sentinel value indicating it has not yet been initialized. Fixes angular#14988 fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null revert: "fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null" This reverts commit f0978db. revert: "fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null" This reverts commit f0978db. fixup! fix(forms): ValueAccessor.writeValue(...) sometimes called with null
`ValueAccessor.writeValue(...)` is called twice on the very first `ngModel` change if the form has not yet been set up; once by `setUpControl` and once when the value is actually updated. However, because `setUpControl` is called before the value is set, the first call passes `null`. The bug is most common for standalone form controls which are never set up before the first `ngModel` change but may also happen if any other form control is not added to its parent first by calling `NgForm.addControl(...)`. Fix that by optionally skipping the `writeValue(...)` call in `setUpControl` if an optional flag is given. Fixes angular#14988
|
I come from the future, it's a bad year. This bug has not been fixed yet. They say to create a condition to detect if the "writeValue" is null. But "writeValue" belongs to the nodeModules, therefore go to "@angular/forms" and modify it, it seems a very bad idea. That I understood. Explain to me if I'm wrong. Sorry for my english, i speak spanish. |
Tu inglés es lo suficientemente bueno como para hacer reír a carcajadas a un hablante nativo, Daniel. Creo que lo que están sugiriendo es modificar el método ValueAccessor.writeValue de su control personalizado para detectar nulo y hacer lo necesario. Me he equivocado antes. |
|
Any progress on this? |
|
I also faced with same issue when using In my case, the second call of |
|
https://stackoverflow.com/questions/39835811/ngmodel-binding-is-null-in-oninit-in-angular-2 |
|
This is getting ridiculous now. When is this getting fixed ? |
|
So is there any progress? |
|
@maxime1992 There is already an open PR, wise guy. It's been open for over a year: #38140 |
@stewx Well, even-wiser-guy, now that PR is closed and this bug remains unfixed after FIVE years. |
|
How to fix it simple and properly? My solution at the moment is just wrap the Another solution I found is to add Would be glad to hear some better solution is any. |
|
In my case. I've noticed that first So my workaround for this bug was this: Create class property for onChange but don't assign a value to it: onChange: (value: unknown) => void;Then on registerOnChange(fn: (value: unknown) => void): void {
this.onChange = fn;
}And check if writeValue(value: unknown): void {
if(this.onChange) {
// your code here
}
}Maybe it will be helpful for someone. |
|
Hey Angular team, I've had time to start a family, see my children grow up, graduate and learn React instead of Angular just to avoid this bug, do you think this is normal? |
|
Wasn't this solved by using the |
|
I'm using Reactive forms on different pages but this issue only occur in One page and code is similar. I've no clue why i'm facing this only on one page. Can anyone help me ? how to solve this |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

I'm submitting a ... (check one with "x")
Current behavior
The ValueAccessor.writeValue method is called twice on a custom value accessor during component initialization, when a control is bound using [(ngModel)], first time with a phantom null value.
Note that this is not an issue in form builder scenarios when control is bound using [formControlName].
Expected behavior
ValueAccessor.writeValue should be called only once, with an actual value of the property bound to [(ngModel)].
Minimal reproduction of the problem with instructions
Run the following plunker:
http://plnkr.co/edit/g5kjqQ9bQ9DF2cs4DbEH?p=preview
MyValueAccessor is applied to the MyCmp component, the latter is bound using [(ngModel)] to the property initialized to 'Hello'. MyValueAccessor.writeValue writes the passed value to the console, the output looks as follows:
What is the motivation / use case for changing the behavior?
Please tell us about your environment:
Language: TypeScript
Node (for AoT issues):
node --version=The text was updated successfully, but these errors were encountered: