I have the function that I need to inject in any component:
export const matDialogCallbacks = (dialog: MatDialog, document: Document, renderer: Renderer2) => {
dialog.afterOpened.subscribe(() => renderer.addClass(document.body, 'overflow-hidden'));
dialog.afterAllClosed.subscribe(() => renderer.removeClass(document.body, 'overflow-hidden'));
};
I need to inject it and use:
constructor(private matDialogCallbacks: matDialogCallbacks, private dialog: MatDialog) {
matDialogCallbacks(dialog);
}
How to do that?
I made the service:
import { Injectable, Inject, Renderer2 } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { DOCUMENT } from '@angular/common';
@Injectable({
providedIn: 'root',
})
export class DialogEventsService {
constructor(@Inject(DOCUMENT) private document: Document, private renderer: Renderer2) {}
init(dialog: MatDialog) {
dialog.afterOpened.subscribe(() => this.renderer.addClass(this.document.body, 'overflow-hidden'));
dialog.afterAllClosed.subscribe(() => this.renderer.removeClass(this.document.body, 'overflow-hidden'));
}
}
When I try to use this service in component I get this:
NullInjectorError: R3InjectorError(OrdersDistributionModule)[DialogEventsService -> DialogEventsService -> Renderer2 -> Renderer2 -> Renderer2]: