Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: ChangeDetector over HttpErrorHandler #208

Open
Zetanova opened this issue May 6, 2021 · 1 comment
Open

Bug: ChangeDetector over HttpErrorHandler #208

Zetanova opened this issue May 6, 2021 · 1 comment
Labels
type: bug Indicates an unintended behaviour, unexpected problem, crash

Comments

@Zetanova
Copy link

Zetanova commented May 6, 2021

If an message gets queued in the the angular ErrorHandler.handleError(error: any) method,
the notification will not get rendered until some other page change.

Workaround:
To display the message instantly a force change detection can be triggered with
(<any>this.notifierContainer).changeDetector.detectChanges();

export class AppComponent implements OnInit {
  @ViewChild(NotifierContainerComponent) notifierContainer!:NotifierContainerComponent;
  
  ngOnInit(): void {
    this.errorService.errors$
      .subscribe(n => {
        this.notifier.show({
          type: 'error',
          message: n
        });
        (<any>this.notifierContainer).changeDetector.detectChanges();
      });
  }
}
@dominique-mueller dominique-mueller added the type: bug Indicates an unintended behaviour, unexpected problem, crash label May 6, 2021
@mvrueden
Copy link

mvrueden commented May 20, 2021

I stumbled upon the same issue and found a fix on the Angular2-Toaster project page.
They describe that you should run the call within an ngZone.run(...) block.

export class AppErrorHandler implements ErrorHandler {
    constructor(
        private toasterService: ToasterService,
        private ngZone : NgZone) { }

    handleError(error: any): void {
        this.ngZone.run(() => {
            this.toasterService.pop('error', "Error", error);
        });  
    }
}

Source: https://github.com/stabzs/Angular2-Toaster#toasts-are-not-displayed-when-popped-from-an-error-handler

This should probably adapted slightly and just be added to the official documentation and we are good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Indicates an unintended behaviour, unexpected problem, crash
Projects
None yet
Development

No branches or pull requests

3 participants