import { ComponentFactoryResolver, ComponentRef, Injectable, Type, ViewContainerRef } from '@angular/core'; import { Observable, Subject } from 'rxjs/Rx'; import { DynamicContainerService } from '../../base/dynamic-container/dynamic-container.service'; import { UniModalComponent } from './modal.component'; @Injectable() export class ModalService extends DynamicContainerService { constructor(cmpFactoryResolver: ComponentFactoryResolver) { super(cmpFactoryResolver); } createModal( component: Type, options = {}, viewContainerRef: ViewContainerRef = this.defaultContainer ): Observable { const modalRef: ComponentRef = this.attachComponent(UniModalComponent, viewContainerRef); modalRef.instance.initContent(component, options); const modalSub = new Subject(); modalRef.instance.cancel.subscribe( ev => { modalSub.error(ev); modalRef.destroy(); } ); modalRef.instance.submit.subscribe( ev => { modalSub.next(ev); modalRef.destroy(); } ); return modalSub.asObservable(); } }