Circular dependency in DI detected for ApplicationRef

See original GitHub issue

I’m submitting a…

[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other...

Current behavior

When trying to inject the HotToastService in a custom ErrorHandler I get the following error:

Error: NG0200: Circular dependency in DI detected for ApplicationRef. Find more at https://angular.io/errors/NG0200
    at throwCyclicDependencyError (core.js:216)
    at R3Injector.hydrate (core.js:11408)
    at R3Injector.get (core.js:11232)
    at injectInjectorOnly (core.js:4728)
    at ɵɵinject (core.js:4732)
    at Object.ViewService_Factory [as factory] (ngneat-overview.js:437)
    at R3Injector.hydrate (core.js:11412)
    at R3Injector.get (core.js:11232)
    at injectInjectorOnly (core.js:4728)
    at ɵɵinject (core.js:4732)

Other components can inject the service without any issues.

Expected behavior

Should be able to inject the service in the custom error handler.

Minimal reproduction of the problem with instructions

Setup the library and the following service:

@Injectable()
export class AppErrorHandlerService implements ErrorHandler {
  constructor(private readonly toast: HotToastService) {}

  handleError(error: any): void {
    this.toast.error('Unexpected error.');
    console.error(error);
  }
}

Provide via { provide: ErrorHandler, useClass: AppErrorHandlerService }.

The issue might’ve been introduced with Angular v12, as another project on v11 has this pattern working.

Environment

"@angular/core": "~12.0.2",
"@ngneat/hot-toast": "^2.0.2",
"@ngneat/overview": "^1.0.0",

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
shhdharmencommented, Jun 8, 2021

You can get HotToastService from Injector:

    import { ErrorHandler, Inject, Injectable, Injector } from '@angular/core';
    import { HotToastService } from '@ngneat/hot-toast';

    @Injectable()
    export class AppErrorHandlerService implements ErrorHandler {
      constructor(@Inject(Injector) private readonly injector: Injector) {}

     /**
     * Need to get HotToastService from injector rather than constructor injection to avoid cyclic dependency error
     * @returns {} 
     */
      private get toast() {
        return this.injector.get(HotToastService);
      }

      handleError(error: any): void {
        this.toast.error(error.message);
        console.error(error);
      }
    }

Ref: https://github.com/scttcper/ngx-toastr/issues/327#issuecomment-361304175

0reactions
wellington1993commented, Sep 12, 2022

You can get HotToastService from Injector:

    import { ErrorHandler, Inject, Injectable, Injector } from '@angular/core';
    import { HotToastService } from '@ngneat/hot-toast';

    @Injectable()
    export class AppErrorHandlerService implements ErrorHandler {
      constructor(@Inject(Injector) private readonly injector: Injector) {}

     /**
     * Need to get HotToastService from injector rather than constructor injection to avoid cyclic dependency error
     * @returns {} 
     */
      private get toast() {
        return this.injector.get(HotToastService);
      }

      handleError(error: any): void {
        this.toast.error(error.message);
        console.error(error);
      }
    }

Ref: scttcper/ngx-toastr#327 (comment)

Saved my day! Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular dependency in DI detected for ApplicationRef. How to ...
I am a beginner in Angular and tried to implement global error handling in the application for this purpose I create appErrorHandler class...
Read more >
NG0200: Circular dependency in DI detected while ... - Angular
Break this loop (or circle) of dependency to resolve this error. This most commonly means removing or refactoring the dependencies to not be...
Read more >
[Debugging] Circular dependency in DI detected - YouTube
In this video, you'll learn what the error " Circular dependency in DI detected " means, how to debug it, and prevent it...
Read more >
“Circular dependency detected” in Angular 11, how to solve it?
Circular Dependency in Angular. Problem. We have one service named SharedService and a component named LoginDialogComponent.
Read more >
Circular dependency in DI detected - ZimBaroo's Geeks Sharing
I have faced two cases of circular dependencies a lot. So, here is the explanation. home.page.ts import { Component } from '@angular/core'; ...
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