ModalHelper 对话框辅助类

基于 NzModalService 封装,它解决一些已知问题:

  • 更友好的处理回调

用法

  1. this.modalHelper.create(FormEditComponent, { i }).subscribe(res => this.load());
  2. // 成功范例
  3. // 1. 视为成功
  4. this.subject.close(true);
  5. this.subject.close({ other: 1 });
  6. // 2. 视为失败
  7. this.subject.close();
  8. // 关闭:
  9. this.subject.destroy();

包括两个方法体 create & createStatic 分别打开普通&静态对话框。在 NzModalService 基础上新增若干参数。

自定义组件HTML模板

  1. <div class="modal-header">
  2. <div class="modal-title">Title</div>
  3. </div>
  4. Your body content
  5. <div class="modal-footer">
  6. <button nz-button [nzType]="'default'" (click)="cancel()">
  7. Cancel
  8. </button>
  9. <button nz-button [nzType]="'primary'" (click)="ok()">
  10. OK
  11. </button>
  12. </div>

API

名称类型默认值描述
size指定对话框大小sm,md,lg,xl,numberlg
exact是否精准(默认:true),若返回值非空值(nullundefined)视为成功,否则视为错误booleantrue
includeTabs是否包裹标签页booleanfalse
modalOptions对话框 ModalOptions 参数ModalOptions-

代码演示

ModalHelper 对话框辅助类 - 图1

基础样例

最简单的用法。

  1. import { Component } from '@angular/core';
  2. import { DrawerHelper } from '@delon/theme';
  3. import { DemoDrawerComponent } from '@shared';
  4. import { NzMessageService } from 'ng-zorro-antd/message';
  5. @Component({
  6. selector: 'theme-modal-simple',
  7. template: `
  8. <button nz-button (click)="open()">Open</button>
  9. <button nz-button (click)="static()">Static</button>
  10. `,
  11. })
  12. export class ThemeModalSimpleComponent {
  13. constructor(private modalHelper: DrawerHelper, private msg: NzMessageService) {}
  14. open(): void {
  15. this.modalHelper.create('View', DemoDrawerComponent, { record: { a: 1, b: '2', c: new Date() } }).subscribe(res => {
  16. this.msg.info(res);
  17. });
  18. }
  19. static(): void {
  20. this.modalHelper.static('View', DemoDrawerComponent, { record: { a: 1, b: '2', c: new Date() } }).subscribe(res => {
  21. this.msg.info(res);
  22. });
  23. }
  24. }