import { Injectable , ComponentRef , ViewContainerRef , Injector , ComponentFactoryResolver } from '@angular/core' ; import { LoadingComponent } from './loading.component' ; @ Injectable ( ) export class LoadingService { public defaultViewContainerRef : ViewContainerRef ; private cmpRef : ComponentRef < LoadingComponent > ; private loaded : boolean ; constructor ( private componentFactoryResolver , private injector ) { } show ( viewContainer ? ) { viewContainer = viewContainer || this . defaultViewContainerRef ; if ( ! this . loaded ) { this . loaded = true ; const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( LoadingComponent ) ; this . cmpRef = viewContainer . createComponent ( componentFactory , viewContainer . length , this . injector ) ; this . cmpRef . instance . show ( ) ; } } hide ( ) { this . loaded = false ; this . cmpRef . destroy ( ) ; } }