Skip to content

Commit 4c7ad5d

Browse files
committed
feat: add a common dialog
1 parent a604dee commit 4c7ad5d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/app/dialog.component.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Component, Input } from '@angular/core';
2+
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
3+
4+
export interface DialogCustomModel {
5+
title: string;
6+
message: string;
7+
yesContent: string;
8+
noContent: string;
9+
yesClass: string;
10+
noClass: string;
11+
}
12+
13+
@Component({
14+
selector: 'app-dialog',
15+
template: `
16+
<div class="modal-header">
17+
<h4 class="modal-title">{{ customModel.title }}</h4>
18+
</div>
19+
<div class="modal-body">
20+
<ng-container>
21+
<p>{{ customModel.message }}</p>
22+
</ng-container>
23+
24+
<ng-content></ng-content>
25+
</div>
26+
<div class="modal-footer">
27+
<button class="btn {{customModel.yesClass}}" (click)="activeModal.close(true)">{{customModel.yesContent}}</button>
28+
<button class="btn {{customModel.noClass}}" (click)="activeModal.close(false)">{{customModel.noContent}}</button>
29+
</div>
30+
`
31+
})
32+
export class DialogComponent {
33+
customModel: DialogCustomModel = null;
34+
constructor(public activeModal: NgbActiveModal) { }
35+
}

src/app/dialog.service.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Injectable } from '@angular/core';
2+
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
3+
import { DialogComponent, DialogCustomModel } from './dialog.component';
4+
5+
@Injectable({ providedIn: 'root' })
6+
export class DialogService {
7+
constructor(private modalService: NgbModal) { }
8+
9+
showCustom(customModel: DialogCustomModel): Promise<boolean> {
10+
if (!customModel.yesContent)
11+
customModel.yesContent = "确定";
12+
if (!customModel.noContent)
13+
customModel.noContent = "取消";
14+
if (!customModel.yesClass)
15+
customModel.yesClass = "btn-primary";
16+
if (!customModel.noClass)
17+
customModel.noClass = "btn-secondary";
18+
19+
const modalRef = this.modalService.open(DialogComponent, { centered: true });
20+
modalRef.componentInstance.customModel = customModel;
21+
22+
return modalRef.result.catch(() => false);
23+
}
24+
25+
show(title: string, message: string): Promise<boolean> {
26+
return this.showCustom({ title, message, yesClass: null, yesContent: null, noClass: null, noContent: null });
27+
}
28+
}

0 commit comments

Comments
 (0)