@@ -78,6 +79,13 @@ let titleDoc = require('html-loader!markdown-loader!./docs/title.md');
+
Events
+
Modal service events. Modal service exposes 4 events: onShow, onShown, onHide, onHidden. See usage example below.
+
onHide and onHidden emit dismiss reason. Possible values are backdrop-click, esc or null if modal was closed by direct call of hide()
+
+
+
+
Options
There are some options that you can configure, like animation, backdrop, closing by Esc button, additional css classes. See the demo below to learn how to configure your modal
diff --git a/src/component-loader/component-loader.class.ts b/src/component-loader/component-loader.class.ts
index aa5fee7c4f..da691cbb20 100644
--- a/src/component-loader/component-loader.class.ts
+++ b/src/component-loader/component-loader.class.ts
@@ -163,6 +163,9 @@ export class ComponentLoader {
const componentEl = this._componentRef.location.nativeElement;
componentEl.parentNode.removeChild(componentEl);
+ if (this._contentRef.componentRef) {
+ this._contentRef.componentRef.destroy();
+ }
this._componentRef.destroy();
if (this._viewContainerRef && this._contentRef.viewRef) {
this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef));
diff --git a/src/modal/bs-modal.service.ts b/src/modal/bs-modal.service.ts
index d15e3179c8..54e23f5d59 100644
--- a/src/modal/bs-modal.service.ts
+++ b/src/modal/bs-modal.service.ts
@@ -1,4 +1,4 @@
-import { ComponentRef, Injectable, TemplateRef } from '@angular/core';
+import { ComponentRef, Injectable, TemplateRef, EventEmitter } from '@angular/core';
import { ComponentLoader } from '../component-loader/component-loader.class';
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory';
@@ -11,14 +11,21 @@ export class BsModalService {
// constructor props
public config: ModalOptions = modalConfigDefaults;
+ public onShow: EventEmitter = new EventEmitter();
+ public onShown: EventEmitter = new EventEmitter();
+ public onHide: EventEmitter = new EventEmitter();
+ public onHidden: EventEmitter = new EventEmitter();
+
protected isBodyOverflowing: boolean = false;
protected originalBodyPadding: number = 0;
+
protected scrollbarWidth: number = 0;
protected backdropRef: ComponentRef;
-
private _backdropLoader: ComponentLoader;
private modalsCount: number = 0;
+ private lastDismissReason: string = '';
+
private loaders: ComponentLoader[] = [];
public constructor(private clf: ComponentLoaderFactory) {
@@ -31,6 +38,7 @@ export class BsModalService {
this._createLoaders();
this.config = Object.assign({}, modalConfigDefaults, config);
this._showBackdrop();
+ this.lastDismissReason = null;
return this._showModal(content);
}
@@ -100,6 +108,10 @@ export class BsModalService {
return this.modalsCount;
}
+ setDismissReason(reason: string) {
+ this.lastDismissReason = reason;
+ }
+
protected removeBackdrop(): void {
this._backdropLoader.hide();
this.backdropRef = null;
@@ -141,7 +153,12 @@ export class BsModalService {
}
private _createLoaders(): void {
- this.loaders.push(this.clf.createLoader(null, null, null));
+ const loader = this.clf.createLoader(null, null, null);
+ this.copyEvent(loader.onBeforeShow, this.onShow);
+ this.copyEvent(loader.onShown, this.onShown);
+ this.copyEvent(loader.onBeforeHide, this.onHide);
+ this.copyEvent(loader.onHidden, this.onHidden);
+ this.loaders.push(loader);
}
private removeLoaders(level: number): void {
@@ -150,4 +167,10 @@ export class BsModalService {
loader.instance.level = i + 1;
});
}
+
+ private copyEvent(from: EventEmitter, to: EventEmitter) {
+ from.subscribe(() => {
+ to.emit(this.lastDismissReason);
+ });
+ }
}
diff --git a/src/modal/modal-container.component.ts b/src/modal/modal-container.component.ts
index cca3419331..2518a1f473 100644
--- a/src/modal/modal-container.component.ts
+++ b/src/modal/modal-container.component.ts
@@ -1,5 +1,5 @@
import { Component, ElementRef, HostListener, OnDestroy, OnInit, Renderer } from '@angular/core';
-import { ClassName, ModalOptions, TransitionDurations } from './modal-options.class';
+import { ClassName, DISMISS_REASONS, ModalOptions, TransitionDurations } from './modal-options.class';
import { BsModalService } from './bs-modal.service';
import { isBs3 } from '../utils/ng2-bootstrap-config';
@@ -30,11 +30,13 @@ export class ModalContainerComponent implements OnInit, OnDestroy {
if (this.config.ignoreBackdropClick || this.config.backdrop === 'static' || event.target !== this._element.nativeElement) {
return;
}
+ this.bsModalService.setDismissReason(DISMISS_REASONS.BACKRDOP);
this.hide();
}
@HostListener('window:keydown.esc')
public onEsc(): void {
if (this.config.keyboard && this.level === this.bsModalService.getModalsCount()) {
+ this.bsModalService.setDismissReason(DISMISS_REASONS.ESC);
this.hide();
}
}
diff --git a/src/modal/modal-options.class.ts b/src/modal/modal-options.class.ts
index 773efb44d5..49b90491d3 100644
--- a/src/modal/modal-options.class.ts
+++ b/src/modal/modal-options.class.ts
@@ -72,3 +72,8 @@ export const TransitionDurations: any = {
MODAL: 300,
BACKDROP: 150
};
+
+export const DISMISS_REASONS = {
+ BACKRDOP: 'backdrop-click',
+ ESC: 'esc'
+};
diff --git a/src/modal/modal.component.ts b/src/modal/modal.component.ts
index 06c12d234c..05e1832e21 100644
--- a/src/modal/modal.component.ts
+++ b/src/modal/modal.component.ts
@@ -21,7 +21,7 @@ import { document } from '../utils/facade/browser';
import { isBs3 } from '../utils/ng2-bootstrap-config';
import { Utils } from '../utils/utils.class';
import { ModalBackdropComponent } from './modal-backdrop.component';
-import { ClassName, modalConfigDefaults, ModalOptions, Selector } from './modal-options.class';
+import { ClassName, modalConfigDefaults, ModalOptions, Selector, DISMISS_REASONS } from './modal-options.class';
import { window } from '../utils/facade/browser';
import { ComponentLoader } from '../component-loader/component-loader.class';
@@ -29,10 +29,6 @@ import { ComponentLoaderFactory } from '../component-loader/component-loader.fac
const TRANSITION_DURATION = 300;
const BACKDROP_TRANSITION_DURATION = 150;
-const DISMISS_REASONS = {
- BACKRDOP: 'backdrop-click',
- ESC: 'esc'
-};
/** Mark any code with directive to show it's content in modal */
@Directive({