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.
No "cyclic dependency" errors.
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.
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.
Closing as a dupe of https://github.com/angular/material2/issues/10967 which will be fixed by https://github.com/angular/material2/pull/10975.
@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._
Most helpful comment
You should be able to do something like this: