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.

Snackbar.open inside ngOnInit produces an ExpressionChangedAfterItHasBeenCheckedError error

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Calling snackbarService.open() should not produce errors, even if the DI has just instantiated the snackbar service.

What is the current behavior?

When calling snackbarService.open() inside ngOnInit, an error is thrown:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '@state: undefined'. Current value: '@state: visible-bottom'. 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 ?

What are the steps to reproduce?

Just add a call to snackbar service’s open, inside ngOnInit https://stackblitz.com/edit/angular-3ma9xb

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 6.0.2, material 6.0.0-rc14

Is there anything else we should know?

I’m guessing that when you inject the service at the constructor, the snackbar div gets appended to the DOM, and if open() gets called too quickly, the change detection is still in progress for the div’s attributes.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
rojasjandro89commented, Mar 22, 2019

That’s an even dirtier hack 🤓…

1reaction
raidenfreemancommented, May 16, 2018

@splitt3r Yes, that’s how I worked around it (small setTimeout). But I guess it feels hacky. 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

ExpressionChangedAfterItHasBe...
If we try to change the binded prop of ngIf before ngAfterViewInit then it wont throw error. i.e. we can set the value...
Read more >
ExpressionChangedAfterItHasBe...
The ExpressionChangedAfterItHasBeenCheckedError error is thrown up when the binding expression changes after being checked by Angular during the ...
Read more >
Everything you need to know about the ... - InDepth.Dev
Everything you need to know about the `ExpressionChangedAfterItHasBeenCheckedError` error. This article explains the underlying causes of the error and the ...
Read more >
UNPKG - @angular/material
_attach(template, config);\n }\n\n /**\n * Opens a snackbar with a message and an optional action.\n * @param message The message to show in...
Read more >
angular/angular - Gitter
you should open your network tab in chrome and verify what is being sent to ... ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: ...
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