writeValue gets called before ngOnInit
See original GitHub issueπ bug report
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 NgControl
is injected into the constructor of a component and the value accessor is set manually to avoid cyclic dependency errors
, writeValue
is called before ngOnInit
.
This workaround is suggested by the Material Angular doc under ngControl
.
π¬ Minimal Reproduction
π Your Environment
Angular Version:
Angular CLI: 7.3.5
Node: 11.9.0
OS: win32 x64
Angular: 7.2.8
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, service-worker
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.5
@angular-devkit/build-angular 0.13.5
@angular-devkit/build-optimizer 0.13.5
@angular-devkit/build-webpack 0.13.5
@angular-devkit/core 7.3.5
@angular-devkit/schematics 7.3.5
@angular/cdk 7.3.3
@angular/cli 7.3.5
@angular/material 7.3.3
@angular/pwa 0.13.5
@ngtools/webpack 7.3.5
@schematics/angular 7.3.5
@schematics/update 0.13.5
rxjs 6.4.0
typescript 3.2.4
webpack 4.29.0
Anything else relevant?
I was sent here by the @angular/material2 team. They believe itβs an issue with @angular/forms
.
https://github.com/angular/material2/issues/15434
Issue Analytics
- State:
- Created 5 years ago
- Reactions:35
- Comments:18 (7 by maintainers)
Top Results From Across the Web
Writevalue Called Before Ngoninit In Nested ... - StackBlitz
Starter project for Angular apps that exports to the Angular CLI.
Read more >controlValueAccessor method writeValue() not being called
writeValue is called when you are changing control value (eg by setting backing ngModel property or controll value) not by user input.
Read more >Angular Custom Form Controls - Complete Guide
The writeValue method is called by the Angular forms module whenever the parent form wants to set a value in the child control....
Read more >Control Value Accessor: Custom Form Components in Angular
writeValue is called when the FormControl value is changed programmatically, like when you call FormControl.setValue(x) . It can receive anything,Β ...
Read more >Better Angular Form Components with ngModel and ...
Angular always looks for an onInit function and tries to call it when the ... and manually check for updates in the writeValue...
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 Free
Top 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
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.
stackblitz
The bug reproduction code is using angular/forms and angular/material libraries. I have removed the material related code from reproduction code, Bug is still coming. So this problem is not because of material library, instead the problem is in angular/form. Now I am debugging angular form to find out the problem.