Components: MatDialog: cyclic dependency when using MatDialog in a HTTP_INTERCEPTORS

Created on 7 Mar 2018  路  8Comments  路  Source: angular/components

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

Most helpful comment

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);
  }
}

All 8 comments

I am facing the exactly similar issue and was not sure how to solve this.
@fredrikredflag were you able to find a fix for this.

@rajpat You can use the Injector to get workaround this issue:

@Injectable()
export class UnauthorizedHook implements HttpInterceptor {
    private _injector: Injector;
    private _reauthorizeWorkflow: ReauthorizeWorkflow;

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

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

Here ReauthorizeWorkflow is using MatDialog.

So how to open/show the ReauthorizeWorkflow dialog?

In my case I have a dialog like this which I want to open from my error handler -

import { Component, Inject, Injectable } from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA, MatDialog } from "@angular/material";

@Component({
selector: "error-dialog-component",
templateUrl: "custom-error-dialog-component.html"
})
export class CustomErrorDialogComponent {
constructor(
private dialogRef: MatDialogRef,
@Inject(MAT_DIALOG_DATA) public data: any
) {}

public closeDialog() {
this.dialogRef.close();
}
}

From my error handler service I want to to open the the CustomErrorDialogComponent which I can't do as I need call open method of matdialog for which I need to have a matdialog injected which what is causing the cyclic issue.

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);
  }
}

That did it.
Thanks for your help.

@fredrikredflag , thank you for the tip, this helped me get around my cyclic dependency issues

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