question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

See original GitHub issue

I need to change the value in the life cycle of the component, but when I do this error falls, what should I do?

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

app.component.ts

import { Component, AfterViewInit } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit {

  public value: boolean = true;
  public testValue = { value: true };

  public ngAfterViewInit() {
    this.value = false;
    this.testValue.value = false;
  }

}

app.component.html

value = {{value}} <br>
testValue.value = {{testValue.value}}
AppComponent.html:1 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.
    at viewDebugError (errors.js:52)
    at expressionChangedAfterItHasBeenCheckedError (errors.js:28)
    at checkBindingNoChanges (util.js:127)
    at checkNoChangesNodeInline (view.js:547)
    at checkNoChangesNode (view.js:520)
    at debugCheckNoChangesNode (services.js:542)
    at debugCheckRenderNodeFn (services.js:486)
    at Object.eval [as updateRenderer] (VM303 AppComponent.ngfactory.js:12)
    at Object.debugUpdateRenderer [as updateRenderer] (services.js:463)
    at checkNoChangesView (view.js:363)

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-frgqqb?file=app%2Fapp.component.ts

Environment

Angular version: 5+

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

30reactions
trotylcommented, Feb 19, 2018

<!-- Please search GitHub for a similar issue or PR before submitting -->

5reactions
NoMercy235commented, Feb 19, 2018

ExpressionChangedAfterItHasBeenCheckedError is thrown when you try to modify a component’s state after the change detection algorithm ended.

Your issue is that ngAfterViewInit happens, as the name suggests after the change detection algorithm ended. At that point, Angular expects nothing else to change in the component’s state until the next tick. However, it does, because you set the value of the properties to false. You can’t something that would trigger a new change detection after a change detection has ended.

You can do those assignments in ngOnInit and Angular won’t complain.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NG0100: Expression has changed after it was checked - Angular
Angular throws an ExpressionChangedAfterItHasBeenCheckedError when an expression value has been changed after change detection has completed.
Read more >
Angular Debugging "Expression has changed": Explanation ...
Learn a complete explanation about ExpressionChangedAfterItHasBeenCheckedError: why it occurs, how to troubleshoot it and how to fix it.
Read more >
Expression ___ has changed after it was checked
In my case I was opening a modal. After open the modal it was showing the message "Expression ___ has changed after it...
Read more >
Fixing "Expression has changed after it was checked" in Angular
The exception appears (in the development mode) at the moment the value is checked and value is different of the updated value. Error...
Read more >
Expression Has Changed After It Was Checked — Angular ...
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'A message for the child component'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found