forked from ng-bootstrap/ng-bootstrap
/
modal-autoclose.component.ts
43 lines (38 loc) · 1.22 KB
/
modal-autoclose.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, TemplateRef} from '@angular/core';
import {ModalDismissReasons, NgbModal, NgbModalRef} from '@ng-bootstrap/ng-bootstrap';
@Component({templateUrl: './modal-autoclose.component.html', changeDetection: ChangeDetectionStrategy.OnPush})
export class ModalAutoCloseComponent {
private modalRef: NgbModalRef = null;
reason = '';
options = {};
constructor(private modalService: NgbModal, private cd: ChangeDetectorRef) {}
openModal(content?: TemplateRef<any>) {
this.modalRef = this.modalService.open(content, this.options);
this.modalRef.result.then(
() => {
this.reason = `Closed`;
this.cd.markForCheck();
},
reason => {
if (reason === ModalDismissReasons.BACKDROP_CLICK) {
this.reason = 'Click';
} else if (reason === ModalDismissReasons.ESC) {
this.reason = 'Escape';
} else {
this.reason = 'Other';
}
this.cd.markForCheck();
});
}
closeModal() {
if (this.modalRef) {
this.modalRef.close();
this.modalRef = null;
}
}
reset() {
this.closeModal();
this.reason = '';
this.options = {};
}
}