import { OverlayContainer } from '@angular/cdk/overlay' ; import { HostListener , Injectable , OnDestroy } from '@angular/core' ; @ Injectable ( { providedIn : 'root' } ) export class CdkOverlayContainer extends OverlayContainer implements OnDestroy { @ HostListener ( 'window:beforeunload' ) ngOnDestroy ( ) { super . ngOnDestroy ( ) ; } getRootElement ( ) { return this . _document . querySelector ( '.overlay-container-parent' ) ; } protected _createContainer ( ) { super . _createContainer ( ) ; this . _appendToRootComponent ( ) ; } private _appendToRootComponent ( ) { if ( ! this . _containerElement ) { return ; } const rootElement = this . getRootElement ( ) ; const parent = rootElement || this . _document . body ; parent . appendChild ( this . _containerElement ) ; } }