Components: Snackbar wrongly positioned if opened from global error handler

Created on 18 Sep 2018  路  3Comments  路  Source: angular/components

Bug, feature request, or proposal:

I use a global error handler in my app which shows a snackbar to the user if a certain type of exception is beeing thrown.
If the exception originates from an async function (in my case a promise handler after a http request), the snackbar will be rendered at a wrong position.
The snackbar will only correct its position if the user triggers the change detection (click, mousemove, etc..).

What are the steps to reproduce?

https://stackblitz.com/edit/angular-material2-issue-td5qwb

  1. Define a global error handler which shows a snackbar.
  2. Throw an (uncaught) exception from an async function.

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

@angular/core 6.0.7
@angular/material 6.3.2
Chrome 69
Windows 7

Is there anything else we should know?

My current workaround is to show an empty snackbar and manually trigger a change detection cycle before showing the actual snackbar:

try {
  this.snackBar.open("");
  this.snackBar.dismiss();
  this.applicationRef.tick();
} catch (error) {}

this.snackBar.open("MESSAGE");

Most helpful comment

Found a solution for my problem and just wanted to post it here so maybe it could help others in the same situation in the future.

The NgZone service allows to execute code within the angular zone. So if i use the NgZone.run method to execute the MatSnackBar.open everything works as expected:
https://stackblitz.com/edit/angular-material2-issue-ldsygb?file=app/global-error-handler.ts

All 3 comments

Closing as duplicate of #7233

Found a solution for my problem and just wanted to post it here so maybe it could help others in the same situation in the future.

The NgZone service allows to execute code within the angular zone. So if i use the NgZone.run method to execute the MatSnackBar.open everything works as expected:
https://stackblitz.com/edit/angular-material2-issue-ldsygb?file=app/global-error-handler.ts

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelb-01 picture michaelb-01  路  3Comments

3mp3ri0r picture 3mp3ri0r  路  3Comments

shlomiassaf picture shlomiassaf  路  3Comments

constantinlucian picture constantinlucian  路  3Comments

xtianus79 picture xtianus79  路  3Comments