angular: [Ivy] ASSERTION ERROR: Should be run in update mode
🐞 bug report
Affected Package
The issue is caused by package @angular/core (Ivy)Is this a regression?
Yes, works in non-Ivy.
Description
I suspect this happens as a result of having an Input property that is set directly (isExpanded="true") instead of using a binding ([isExpanded]="true"), and having the property setter call changeDetectorRef.detectChanges(). It should be noted that this works in non-ivy.
🔬 Minimal Reproduction
https://github.com/fr0/angular-test/tree/ivy
- Clone repo
git checkout ivynpm installnpm start- Open http://localhost:4200 in Chrome
- View browser console log
🔥 Exception or Error
core.js:5829 ERROR Error: ASSERTION ERROR: Should be run in update mode
at throwError (core.js:1164)
at assertEqual (core.js:1123)
at refreshView (core.js:12425)
at detectChangesInternal (core.js:13907)
at ViewRef.detectChanges (core.js:15373)
at ExpandableIfComponent.set isExpanded [as isExpanded] (expandable-if.component.ts:60)
at setInputsFromAttrs (core.js:13579)
at postProcessDirective (core.js:13335)
at instantiateAllDirectives (core.js:13234)
at createDirectivesInstances (core.js:12618)
🌍 Your Environment
Angular Version:
Angular CLI: 9.0.0-next.5
Node: 12.6.0
OS: darwin x64
Angular: 9.0.0-next.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.0-next.5
@angular-devkit/build-angular 0.900.0-next.5
@angular-devkit/build-optimizer 0.900.0-next.5
@angular-devkit/build-webpack 0.900.0-next.5
@angular-devkit/core 9.0.0-next.5
@angular-devkit/schematics 9.0.0-next.5
@angular/cli 9.0.0-next.5
@ngtools/webpack 9.0.0-next.5
@schematics/angular 9.0.0-next.5
@schematics/update 0.900.0-next.5
ng-packagr 5.5.1
rxjs 6.5.3
typescript 3.5.3
webpack 4.40.2
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 19
- Comments: 33 (5 by maintainers)
Commits related to this issue
- fix runtime assertion error https://github.com/angular/angular/issues/32756#issuecomment-629711119 — committed to michalakadam/perfect_house by michalakadam 6 years ago
oh, the same issue (angular 9.1.7 with { ngZone: ‘noop’ })
resolved it by moving logic out of constructor to ngOnInit
@BzenkoSergey It worked! You are a champ!
Looked through my company’s project for ALL logic defined in components constructors, and moved anything I found to ngOnInit, and bam, no error.
I use setTimeout(() => this.changeDetector.detectChanges(), 0);
Temporary fix for me: replace call
detectChanges()tomarkForCheck().same problem. any updates?
Please provide minimal reproduction with exact steps on how to reproduce it. We have limited time and can’t go explore your application.
A typical scenario, where this error occurs is something along those lines:
Basically we are invoking an input setter in the creation mode (this might happen if an input is set from a static attribute). 2 possible solutions here:
In Angular 12 this is happening even when not in a constructor. Simply calling .detectChanges() in an observable that, I don’t know…initially runs too early and fast?, throws this error. Constructor has nothing in the body.
This error appeared when I was calling detectChanges() in the constructor after subscribing to some data. I moved the subscription and detectChanges() to ngOnInit() instead and it disappeared.
I believe this is bad practice to have some logic within constructor in any case. But yeah, worked for me.
PR is still open https://github.com/angular/angular/pull/33463
If you’re running into this when writing tests and mocking a normally async call using
of(yourMockedResult), it could be because your component’s code is subscribing to the observable in the constructor and callingchangeDetectorRef.detectChanges()in the callback. Because the call is async in your application, this does not pose an issue when running it, but mocking the observable usingof(...)is making it sync in this case, which is why Angular complains. A pragmatic solution: useof(yourMockedResult).pipe(delay(0))instead, which forces the observable to be async. That way, thechangeDetectorRef.detectChanges()call will be pushed further along in the event loop, giving Angular a chance to put everything in place. (That said, it’s probably a good idea to move it tongOnInitif you can.)This breaks a big chunk of our application, any updates on this issue?
Same issue here. I experienced the issue using
this.changeDetectorRef.detectChanges()in a sync operation.Some background: I’m building an Electron Application using Angular 9.0.6 + Electron 8 + @angular-guru/electron-builder (as builder)
Using the code below triggers the error:
Removing that line, or changing to as below, solved my problem:
Hope this could be helpful for someone. My environment: