I have a component which I'm trying to use from 2 places
<comp-a></comp-a>
.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
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:
notFoundValue
of Injector.get
when we are doing DI through constructor?Any suggestion or explanation will be helpful
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With