demo/src/app/components/+modal/demos/service-events/service-events.ts
import { ChangeDetectorRef, Component, TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/combineLatest';
@Component({
selector: 'demo-modal-service-events',
templateUrl: './service-events.html',
styles: [`
.card {
margin-bottom: 0.75rem;
padding: 8px;
}
`]
})
export class DemoModalServiceEventsComponent {
modalRef: BsModalRef;
subscriptions: Subscription[] = [];
messages: string[] = [];
constructor(private modalService: BsModalService, private changeDetection: ChangeDetectorRef) {
}
openModal(template: TemplateRef<any>) {
this.messages = [];
const _combine = Observable.combineLatest(
this.modalService.onShow,
this.modalService.onShown,
this.modalService.onHide,
this.modalService.onHidden
).subscribe(() => this.changeDetection.markForCheck());
this.subscriptions.push(
this.modalService.onShow.subscribe((reason: string) => {
this.messages.push(`onShow event has been fired`);
})
);
this.subscriptions.push(
this.modalService.onShown.subscribe((reason: string) => {
this.messages.push(`onShown event has been fired`);
})
);
this.subscriptions.push(
this.modalService.onHide.subscribe((reason: string) => {
const _reason = reason ? `, dismissed by ${reason}` : '';
this.messages.push(`onHide event has been fired${_reason}`);
})
);
this.subscriptions.push(
this.modalService.onHidden.subscribe((reason: string) => {
const _reason = reason ? `, dismissed by ${reason}` : '';
this.messages.push(`onHidden event has been fired${_reason}`);
this.unsubscribe();
})
);
this.subscriptions.push(_combine);
this.modalRef = this.modalService.show(template);
}
unsubscribe() {
this.subscriptions.forEach((subscription: Subscription) => {
subscription.unsubscribe();
});
this.subscriptions = [];
}
}