Michał Ziobro
Michał Ziobro

Reputation: 11812

Angular 4: manual injection of dependencies with Injector

I am trying to inject manually MAT_DIALOG_DATA dependency for Angular Material Dialog. I would like to reuse my component both inside dialog and as a regular component embedded in html. So I cannot use constructor injection as I need to conditionally inject this dependencies only If the component is inside dialog.

constructor(private injector : Injector,  
                private activatedRoute : ActivatedRoute, 
                private fb : FormBuilder, 
                private userService : UserService) {

        this.resetToken = activatedRoute.snapshot.params['resetToken']; 
        if(this.hasResetToken()) { 
             this.dialogRef  = this.injector.get(MatDialogRef);
             this.data = this.injector.get(MAT_DIALOG_DATA); 
        }

The above MatDialogRef works correctly, but MAT_DIALOG_DATA InjectionToken doesn't work.

Upvotes: 0

Views: 773

Answers (1)

Michał Ziobro
Michał Ziobro

Reputation: 11812

This injection works correctly. I have an error in hasResetToken() check related to undefined variable.

Upvotes: 0

Related Questions