Search code examples
angulartypescriptangular-material

Moving @Inject(MAT_DIALOG_DATA) out of class constructors


I am changing the Dependency Injection pattern we use so the team minimise the use of Typescript constructors. This avoids some recurring issues we get with people adding logic which should not be in constructors.

We can easily move from this pattern:

constructor(private readonly fb: FormBuilder) {}

to this one:

private readonly fb = inject(FormBuilder);

But we could not find a way to move the following out of the constructors

constructor(@Inject(MAT_DIALOG_DATA) public data: string ) {}

How could it be done?


Solution

  • You can use the new inject function also to inject injection tokens.

    i.e.

    private readonly data: string = inject(MAT_DIALOG_DATA);