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.

MatDialog: cyclic dependency when using MatDialog in a HTTP_INTERCEPTORS

See original GitHub issue

Bug (or odd edge case):

I use MatDialog in a HTTP_INTERCEPTORS to display a dialog if the HttpClient responds with a 401. This start causing “cyclic dependency” errors when HttpClient is a dependency for APP_INITIALIZER.

What is the expected behavior?

No “cyclic dependency” errors.

What is the current behavior?

This might happen due to the Overlay (used in MatDialog constructor) injecting the ApplicationRef that’s already imported by ApplicationModule . This error is thrown during resolving of deps for APP_INITIALIZER.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-material2-issue-7vxrxf

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
fredrikredflagcommented, Mar 20, 2018

You should be able to do something like this:

@Injectable()
export class UnauthorizedHook implements HttpInterceptor {
    private _dialog: MatDialog;
    private _injector: Injector;

    constructor (
        injector: Injector
    ) {
        this._injector = injector;
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        if (!this._dialog) {
            this._dialog = this._injector.get(MatDialog);
        }

        this._dialog.open(CustomErrorDialogComponent);
  }
}
1reaction
crisbetocommented, Apr 25, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular Dependancy in Angular-Material Dialog Module
Both modals are opened from the authService. The problem is that the authService needs a reference to the login and signup components in...
Read more >
[Solved]-Cannot instantiate cyclic dependency ...
Coding example for the question Cannot instantiate cyclic dependency! HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule CoreModule-rx.js.
Read more >
Services, Interceptors, and Cyclic Dependencies : r/angular
I've fallen into the mud of some cyclic dependency that has to do with HttpInterceptors and can't figure out how to work around...
Read more >
Angular 4: Cannot Instantiate Cyclic Dependency ... - ADocLib
core js circular dependency If you use predominantly constructor ... create cyclic dependency, there should be service from inside Angular HttpInterceptor I ...
Read more >
NG0200: Circular dependency in DI detected while ... - Angular
Debugging the errorlink. Use the call stack to determine where the cyclical dependency exists. You will be able to see if any child...
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