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
writeValue gets called before ngOnInit #29218
Comments
|
Hi @romansattler, it's a unclear to me why writeValue should be called before ngOnInit. It currently behaves like other inputs would, i.e. that value gets set before ngOnInit: example. It seems like this would be working as intended. |
|
For the NgControl case, this is not behaviorally consistent. If you use NG_VALUE_ACCESSOR for the control value accessor, then ngOnInit is called before writeValue, whereas with the new way that @romansattler pointed out, ngOnInit is called after writeValue. This is likely where he discovered the bug and definitely caused us a lot of headaches as well. |
|
Any update on this? I experience the same issue, the workaround I use is to add |
|
Currently ran into this issue recently when initialising a form group via FormBuilder which is made up of custom reactive form controls, one using the NgControl constructor injection approach instead of the usual provide NG_VALUE_ACCESSOR approach. In the writeValue function theres some logic to target the ngControl.control which gets executed when we call a setValue() on the control with an initial value we get from an API call which is being ran before the ngControl.control has had time to initialise (the ngControl has been initialised however). A work around I've done is override the original writeValue() and check the ngControl.control is present prior to it being accessed. |
|
I have encountered the same issue when injecting NgControl. In writeValue I don't have my inner form initialized just yet since it needs some info from ngControl.control which I can obtain only in ngOnInit. |
|
I would like to be volunteer for this bug. Please assign this bug to me . grajeshg2000 |
|
Thanks @grajeshg2000 - please coordinate with @AndrewKushnir on any fix you would propose. |
|
The bug reproduction code is using angular/forms and angular/material libraries. |
|
Hi @grajeshg2000, could you please share a link to a simplified repro (without Material), so we can have a look? Thank you. |
|
code lies in https://stackblitz.com/edit/angular-ivy-2rrugb |
@grajeshg2000I can't see any |
|
I have the same issue. Any update? |
|
I have the same issue as well. My component implements ControlValueAccessor, injects ngControl in the constructor, and sets ngControl.valueAccessor = this . I don't specify providers in order to avoid the error for circular references due to having multiple valueAccessors. When trying to initial variables from ViewChild, I cache a reference for using in the renderer, for example: ngAfterViewInit() { If I try to use this.inputEl in the writeValue method, it throws an undefined error for inputEl. As a workaround, I use the following in the writeValue method which is to just use input.nativeElement. writeValue(value: any): void { |
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
the this._onChange null check here is necessary because of an ongoing bug in angular forms where writeValue can be called before the component initializes: angular/angular#29218
|
The real problem here is that you can't both provide |
|
I am too facing this issue on a custom control. Any current alternatives to the |
not sure, but how about injecting the Injector itself into the constructor and getting ngControl (FormControl) through it and use providers as usual? It's a dirty fix, but it seems to work. |
That's the approach I'm following but for some reason See example app: https://stackblitz.com/edit/angular-ivy-mmcdey?file=src/app/custom-input-ngcontrol/custom-input-ngcontrol.component.html |
|
It should always respect the same order of execution before or after init Lifecycle hooks (ngOnInit, ngAfterViewInit, etc) but always consistent |
|
As a fun bonus of this angular 15 change |
romansattler commentedMar 11, 2019
Affected Package
The issue is caused by package `@angular/forms`Is this a regression?
I don't know. I don't think so.
Description
When
NgControlis injected into the constructor of a component and the value accessor is set manually to avoidcyclic dependency errors,writeValueis called beforengOnInit.This workaround is suggested by the Material Angular doc under
ngControl.Stackblitz
Angular Version:
Anything else relevant?
I was sent here by the @angular/material2 team. They believe it's an issue with
@angular/forms.angular/components#15434
The text was updated successfully, but these errors were encountered: