Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
See original GitHub issueReceiving the following error:
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: ‘undefined’. Current value: ‘toast-success toast’. It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ? at viewDebugError (core.es5.js:8420) at expressionChangedAfterItHasBeenCheckedError (core.es5.js:8398) at checkBindingNoChanges (core.es5.js:8562) at checkNoChangesNodeInline (core.es5.js:12423) at checkNoChangesNode (core.es5.js:12397) at debugCheckNoChangesNode (core.es5.js:13174) at debugCheckRenderNodeFn (core.es5.js:13114) at Object.eval [as updateRenderer] (Toast_Host.html:1) at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13096) at checkNoChangesView (core.es5.js:12219)
This is caused by the following code:
import { ToastrService } from 'ngx-toastr';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
constructor(private toastr: ToastrService) { }
public ngOnInit(): void {
this.toastr.success("testing", "testing");
}
}
This only seems to happen when a toast is created automatically on load. Most likely because not everything is loaded yet?
Doesn’t happen when everything is loaded (ex: on button press)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:12 (3 by maintainers)
Top GitHub Comments
call it inside a setTimeout
I am aware that this issue was closed without a solution or a proper solution. I believe sharing this could be helpful. It seems like the issue has something to do with angular life cycle hooks. I am yet to find time to learn about the details. For my case I moved
this.toastrService.success(this.message);
fromngOnInit()
toconstructor()
as recommended here: https://stackoverflow.com/questions/43375532/expressionchangedafterithasbeencheckederror-explained