Yet another ExpressionChangedAfterItHasBeenCheckedError
See original GitHub issueπ bug report
Affected Package
The issue is caused by package @angular/core (?)Is this a regression?
I had a similar "flow" in an Angular v8 project and didn't encounter the bug but I'm not sure at all as this bug appeared on a brand new project.Description
When switching a mat-progress bar mode according to a boolean (or by displaying one or another progress bar with *ngIf), the following error pops in the console, either when loading the app or changing route:ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'mode: determinate'. Current value: 'mode: indeterminate'.
(or this one for the *ngIf version:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true'.
)
π¬ Minimal Reproduction
https://stackblitz.com/edit/angular-t7ecan?file=src%2Fapp%2Fprogress-bar-test.htmlπ₯ Exception or Error
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'mode: determinate'. Current value: 'mode: indeterminate'.
at viewDebugError (errors.ts:38)
at expressionChangedAfterItHasBeenCheckedError (errors.ts:24)
at checkBindingNoChanges (util.ts:104)
at checkNoChangesNodeInline (view.ts:485)
at checkNoChangesNode (view.ts:472)
at debugCheckNoChangesNode (services.ts:468)
at debugCheckDirectivesFn (services.ts:396)
at Object.eval [as updateDirectives] (VM28592 ProgressBarTest.ngfactory.js:47)
at Object.debugUpdateDirectives [as updateDirectives] (services.ts:388)
at checkNoChangesView (view.ts:352)
π Your Environment
Angular Version:
Angular CLI: 9.1.7
Node: 10.16.2
OS: win32 x64
Angular: 9.1.9
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router, service-worker
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.901.7
@angular-devkit/build-angular 0.901.7
@angular-devkit/build-optimizer 0.901.7
@angular-devkit/build-webpack 0.901.7
@angular-devkit/core 9.1.7
@angular-devkit/schematics 9.1.7
@angular/cdk 9.2.4
@angular/cli 9.1.7
@angular/material 9.2.4
@ngtools/webpack 9.1.7
@schematics/angular 9.1.7
@schematics/update 0.901.7
rxjs 6.5.5
typescript 3.8.3
webpack 4.42.0
Anything else relevant?
Maybe itβs Material related?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
ExpressionChangedAfterItHasBe...
The infamous ExpressionChangedAfterItHasBeenCheckedError error is among the most common errors encountered in Angular applications.
Read more >ExpressionChangedAfterItHasBe...
1) Use changeDetectorRef Β· 2) use setTimeOut. This will execute your code in another VM as a macro-task. Angular will not see these...
Read more >Everything you need to know about the ... - InDepth.Dev
Yet it has and so Angular throws the error ExpressionChangedAfterItHasBeenCheckedError . The same holds for the third operation. If the name property wasΒ ......
Read more >ExpressionChangedAfterItHasBe...
ExpressionChangedAfterItHasBeenCheckedError is thrown when an expression in your HTML has changed after Angular has checked it (it is a very expressive error).
Read more >Angular Debugging "Expression has changed": Explanation ...
The Angular Development mode; Debugging Techniques for finding the template ... If we now reload the component and trigger the error again,Β ...
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
@Booster2ooo Sure, if you make it async, you always postpone the execution to another tick.
But your original problem is from 99% a symptom of an inappropriate logic in your code. Simply you shouldnβt make the change during one CD cycle - when the component and its childs are checked.
The error seems to be caused by the following: The
ngOnInit()
in the child component gets called AFTER the view of the parent component has been checked already:I added some console.logs to the example provided above and this is the result:
The return value log is from the
loadingService.loading
getter.If I move
this.loadingService.startLoading()
into the parentsngOnInit
then it is working, because it is changing before the view is checked.The only solution I could think of is resolving this issue is using
ChangeDetectorRef
or by making sure, that Angular does not check the View before the ngOnInits of the child components have returned.