Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use same component in normal way and with angular material dialog both? [closed]

Tags:

angular

I have a component which I'm trying to use from 2 places

  1. Normally, including the selector in some template for example <comp-a></comp-a>.
  2. Inside of angular material dialog.

When using the same component with angular material dialog, I have to inject the below dependencies

  constructor(
    public dialogRef: MatDialogRef<CTConfigurationComponent>,
    @Inject(MAT_DIALOG_DATA) public dialogData: any,
  ) {}

It is working fine with case 2, but case 1 is breaking with below error enter image description here

Tried using @Optional(), @Skip() but no success.

Question -1 : 1. Is it possible to tell angular DI to skip few dependencies?

Tried public injector: @Injector, and in constructor calling

this.dialogRef = this.injector.get(MatDialogRef<CTConfigurationComponent>);

is also not working.


EDIT-1:

(method) Injector.get(token: any, notFoundValue?: any)

Question - 2:

Is there any similar thing like notFoundValue of Injector.get when we are doing DI through constructor?

Any suggestion or explanation will be helpful

like image 495
Vivek Kumar Avatar asked Dec 03 '22 18:12

Vivek Kumar


1 Answers

After trying lots of things, I have got a workaround for this error. Posting the solution here so that it may be helpful to someone else

so instead of this

  constructor(
    public dialogRef: MatDialogRef<CTConfigurationComponent>,
    @Inject(MAT_DIALOG_DATA) public dialogData: any
  ) {}

I am using below code

  private dialogRef = null;
  private  dialogData;
  constructor(private injector: Injector) {
      this.dialogRef = this.injector.get(MatDialogRef, null);
      this.dialogData = this.injector.get(MAT_DIALOG_DATA, null);
  }
like image 138
Vivek Kumar Avatar answered May 09 '23 05:05

Vivek Kumar