Skip to content

Instantly share code, notes, and snippets.

@lydemann
Created July 28, 2018 09:42
Show Gist options
  • Save lydemann/587eaa2bc686094226e6bd02fe53ef9a to your computer and use it in GitHub Desktop.
Save lydemann/587eaa2bc686094226e6bd02fe53ef9a to your computer and use it in GitHub Desktop.
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { Injectable } from '@angular/core';
import { SpinnerOverlayComponent } from '@app/core/spinner-overlay/spinner-overlay.component';
@Injectable({
providedIn: 'root'
})
export class SpinnerOverlayService {
private overlayRef: OverlayRef = null;
constructor(private overlay: Overlay) {}
public show(message = '') {
// Returns an OverlayRef (which is a PortalHost)
if (!this.overlayRef) {
this.overlayRef = this.overlay.create();
}
// Create ComponentPortal that can be attached to a PortalHost
const spinnerOverlayPortal = new ComponentPortal(SpinnerOverlayComponent);
const component = this.overlayRef.attach(spinnerOverlayPortal); // Attach ComponentPortal to PortalHost
}
public hide() {
if (!!this.overlayRef) {
this.overlayRef.detach();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment