Modal对话框

用作显示系统的重要信息,并请求用户进行操作反馈,eg:删除某个重要内容时,弹出 Modal 进行二次确认。

规则

  • 尽可能少用。Modal 会打断用户操作,只用在重要的时候。
  • 标题应该简明,不能超过 1 行;描述内容应该简明、完整,一般不多于 2 行。
  • 操作按钮最多到 3 个(竖排),一般为 1-2 个(横排);3 个以上建议使用组件 ActionSheet 来完成。
  • 一般将用户最可能点击的按钮,放在右侧。另外,取消按钮应当始终放在左侧。

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. import { ModalService } from 'ng-zorro-antd-mobile';
  3. @Component({
  4. selector: 'demo-modal-basic',
  5. template: `
  6. <WingBlank>
  7. <WhiteSpace></WhiteSpace>
  8. <div Button (onClick)="showModal('modal1')">text only</div>
  9. <WhiteSpace></WhiteSpace>
  10. <Modal [(ngModel)]="this.state.modal1" [transparent]="true" [title]="'Title'" [footer]="footer">
  11. <div [ngStyle]="{ height: 100, overflow: 'scroll' }">
  12. scoll content... <br />
  13. scoll content... <br />
  14. scoll content... <br />
  15. scoll content... <br />
  16. scoll content... <br />
  17. scoll content... <br />
  18. </div>
  19. </Modal>
  20. <div Button (onClick)="showModal('modal2')">popup</div>
  21. <WhiteSpace></WhiteSpace>
  22. <Modal [(ngModel)]="this.state.modal2" [popup]="true" [animationType]="'slide-up'" (onClose)="onClose('modal2')">
  23. <List [renderHeader]="renderHeader" [className]="'popup-list'">
  24. <ListItem>股票名称</ListItem>
  25. <ListItem>股票代码</ListItem>
  26. <ListItem>买入价格</ListItem>
  27. <ListItem> <div Button [type]="'primary'" (onClick)="onClose('modal2')">买入</div> </ListItem>
  28. </List>
  29. </Modal>
  30. <div Button (onClick)="showModal('modal3')">maskClosable</div>
  31. <WhiteSpace></WhiteSpace>
  32. <Modal
  33. [(ngModel)]="this.state.modal3"
  34. [transparent]="true"
  35. [title]="'Title'"
  36. [maskClosable]="true"
  37. (onClose)="onClose('modal3')"
  38. >
  39. <div [ngStyle]="{ height: 100, overflow: 'scroll' }">
  40. scoll content... <br />
  41. scoll content... <br />
  42. scoll content... <br />
  43. </div>
  44. </Modal>
  45. <div Button (onClick)="showModal('modal4')">closable</div>
  46. <WhiteSpace></WhiteSpace>
  47. <Modal
  48. [(ngModel)]="this.state.modal4"
  49. [transparent]="true"
  50. [title]="'Title'"
  51. [closable]="true"
  52. (onClose)="onClose('modal4')"
  53. >
  54. <div [ngStyle]="{ height: 100, overflow: 'scroll' }">
  55. scoll content... <br />
  56. scoll content... <br />
  57. scoll content... <br />
  58. </div>
  59. </Modal>
  60. </WingBlank>
  61. `,
  62. styles: [
  63. `
  64. .popup-list .am-list-body {
  65. height: 210px;
  66. overflow: auto;
  67. }
  68. `
  69. ]
  70. })
  71. export class DemoModalBasicComponent {
  72. state = {
  73. modal1: false,
  74. modal2: false,
  75. modal3: false,
  76. modal4: false
  77. };
  78. footer = [
  79. {
  80. text: 'Ok',
  81. onPress: () => {
  82. console.log('ok');
  83. this.onClose('modal1');
  84. }
  85. }
  86. ];
  87. footer2 = [
  88. {
  89. text: 'Ok',
  90. onPress: () => {
  91. console.log('ok');
  92. this.onClose('modal2');
  93. }
  94. }
  95. ];
  96. constructor() {}
  97. modelChange(event) {
  98. console.log('asdfasdf', event);
  99. }
  100. onClose(key) {
  101. this.state[key] = false;
  102. }
  103. showModal(key) {
  104. this.state[key] = true;
  105. }
  106. renderHeader() {
  107. return '委托买入';
  108. }
  109. }

警告弹窗

包含无按钮, 确认框, 多按钮情况。

  1. import { Component } from '@angular/core';
  2. import { ModalService, ToastService, ModalRef } from 'ng-zorro-antd-mobile';
  3. @Component({
  4. selector: 'demo-modal-alert',
  5. template: `
  6. <WingBlank>
  7. <WhiteSpace></WhiteSpace>
  8. <div Button (onClick)="showAlert()">customized buttons</div>
  9. <WhiteSpace></WhiteSpace>
  10. <div Button (onClick)="showAlertMuchButtons(message)">More than two buttons</div>
  11. <WhiteSpace></WhiteSpace>
  12. <ng-template #message>
  13. <div>More than two buttons</div>
  14. </ng-template>
  15. <div Button (onClick)="showPromise()">promise</div>
  16. <WhiteSpace></WhiteSpace>
  17. </WingBlank>
  18. `
  19. })
  20. export class DemoModalAlertComponent {
  21. constructor(private _modal: ModalService, private _toast: ToastService) {}
  22. showAlert() {
  23. this._modal.alert('Delete', 'Are you sure ?', [
  24. { text: 'Cancel', onPress: () => console.log('cancel') },
  25. { text: 'OK', onPress: () => console.log('ok') }
  26. ]);
  27. }
  28. showAlertMuchButtons(message) {
  29. this._modal.alert('Much Buttons', message, [
  30. { text: 'Button1', onPress: () => console.log('第0个按钮被点击了') },
  31. { text: 'Button2', onPress: () => console.log('第1个按钮被点击了') },
  32. { text: 'Button2', onPress: () => console.log('第2个按钮被点击了') }
  33. ]);
  34. }
  35. showPromise() {
  36. this._modal.alert('Delete', 'Are you sure???', [
  37. { text: 'Cancel', onPress: () => console.log('cancel') },
  38. {
  39. text: 'Ok',
  40. onPress: () =>
  41. new Promise(resolve => {
  42. this._toast.info('onPress Promise', 1000);
  43. setTimeout(resolve, 1000);
  44. }),
  45. style: {
  46. color: '#ffffff',
  47. background: '#00ff00'
  48. }
  49. }
  50. ]);
  51. }
  52. }

输入弹窗

包含输入普通文字, 密码, 登录信息样式。

  1. import { Component } from '@angular/core';
  2. import { ModalService, ToastService } from 'ng-zorro-antd-mobile';
  3. @Component({
  4. selector: 'demo-modal-prompt',
  5. template: `
  6. <WingBlank>
  7. <WhiteSpace></WhiteSpace>
  8. <div Button (onClick)="showPromptPromise()">promise</div>
  9. <WhiteSpace></WhiteSpace>
  10. <div Button (onClick)="showPromptDefault()">defaultValue</div>
  11. <WhiteSpace></WhiteSpace>
  12. <div Button (onClick)="showSecure()">secure-text</div>
  13. <WhiteSpace></WhiteSpace>
  14. <div Button (onClick)="showCustom()">custom buttons</div>
  15. <WhiteSpace></WhiteSpace>
  16. <div Button (onClick)="showLogin()">login-password</div>
  17. <WhiteSpace></WhiteSpace>
  18. </WingBlank>
  19. `
  20. })
  21. export class DemoModalPromptComponent {
  22. constructor(private _modal: ModalService, private _toast: ToastService) {}
  23. showPromptPromise() {
  24. this._modal.prompt(
  25. 'input name',
  26. 'please input your name',
  27. [
  28. {
  29. text: 'Close',
  30. onPress: value =>
  31. new Promise(resolve => {
  32. this._toast.info('onPress promise resolve', 1000);
  33. setTimeout(() => {
  34. resolve();
  35. console.log(`value:${value}`);
  36. }, 1000);
  37. })
  38. },
  39. {
  40. text: 'Hold on',
  41. onPress: value =>
  42. new Promise((resolve, reject) => {
  43. this._toast.info('onPress promise reject', 1000);
  44. setTimeout(() => {
  45. // reject();
  46. console.log(`value:${value}`);
  47. }, 1000);
  48. })
  49. }
  50. ],
  51. 'default',
  52. null,
  53. ['input your name']
  54. );
  55. }
  56. showPromptDefault() {
  57. this._modal.prompt(
  58. 'defaultValue',
  59. 'defaultValue for prompt',
  60. [{ text: 'Cancel' }, { text: 'Submit', onPress: value => console.log(`输入的内容:${value}`) }],
  61. 'default',
  62. ['100']
  63. );
  64. }
  65. showSecure() {
  66. this._modal.prompt('Password', 'Password Message', password => console.log(`password: ${password}`), 'secure-text');
  67. }
  68. showCustom() {
  69. this._modal.prompt(
  70. 'Password',
  71. 'You can custom buttons',
  72. [{ text: '取消' }, { text: '提交', onPress: password => console.log(`密码为:${password}`) }],
  73. 'secure-text'
  74. );
  75. }
  76. showLogin() {
  77. this._modal.prompt(
  78. 'Login',
  79. 'Please input login information',
  80. (login, password) => console.log(`login: ${login}, password: ${password}`),
  81. 'login-password',
  82. ['default', '123456'],
  83. ['Please input name', 'Please input password']
  84. );
  85. }
  86. }

操作弹窗

操作对话框。

  1. import { Component } from '@angular/core';
  2. import { ModalService } from 'ng-zorro-antd-mobile';
  3. @Component({
  4. selector: 'demo-modal-operation',
  5. template: `
  6. <WingBlank>
  7. <WhiteSpace></WhiteSpace>
  8. <div Button (onClick)="showOpeartion()">operation</div>
  9. <WhiteSpace></WhiteSpace>
  10. </WingBlank>
  11. `
  12. })
  13. export class DemoModalOperationComponent {
  14. constructor(private _modal: ModalService) {}
  15. showOpeartion() {
  16. this._modal.operation([
  17. { text: '标为未读', onPress: () => console.log('标为未读被点击了') },
  18. { text: '置顶聊天', onPress: () => console.log('置顶聊天被点击了') }
  19. ]);
  20. }
  21. }

Modal对话框 - 图1

API

Modal

参数说明类型默认值
[ngModel]对话框是否可见booleanfalse
[closable]是否显示关闭按钮booleanfalse
[maskClosable]点击蒙层是否允许关闭booleantrue
[transparent]是否背景透明booleanfalse
[popup]是否弹窗模式booleanfalse
[animationType]动画类型‘slide-down’ | ‘slide-up’ | ‘fade’ | ‘slide’‘fade’
[title]标题TemplateRef-
[footer]底部内容Array<text: string, onPress: Function>[]
[platform]设定组件的平台特有样式, 仅限web‘android’ | ‘ios’‘ios’
(onClose)点击 x 或 mask 回调EventEmitter<void>-

ModalSerivce.alert(title, message, actions?, platform?)

参数说明类型默认值
[title]标题string | TemplateRef-
[message]提示信息string | TemplateRef-
[actions]按钮组Array<text: string, onPress: Function, style: object>-
[platform]设定组件的平台特有样式, 仅限web‘android’ | ‘ios’‘ios’

ModalSerivce.alert(title, message, actions?, platform?).close() 可以在外部关闭 Alert

ModalSerivce.prompt(title, message, callbackOrActions, type?, defaultValue?, placeholders?, platform?)

参数说明类型默认值
[title]标题string | TemplateRef-
[message]提示信息string | TemplateRef-
[callbackOrActions]按钮组或回调函数Array<text: string, onPress: Function> | Function-
[type]prompt 的样式‘default’ | ‘secure-text’ | ‘login-password’‘default’
[defaultValue]defaultValuestring[]-
[placeholders]placeholdersstring[]-
[platform]设定组件的平台特有样式, 仅限web‘android’ | ‘ios’‘ios’

ModalSerivce.prompt(title, message, callbackOrActions, type?, defaultValue?, placeholders?, platform?).close() 可以在外部关闭 prompt`

ModalSerivce.operation(actions?, platform?)

参数说明类型默认值
[actions]按钮组Array<text: string, onPress: Function, style: object>-
[platform]设定组件的平台特有样式, 仅限web‘android’ | ‘ios’‘ios’

ModalSerivce.operation(actions?, platform?).close() 可以在外部关闭 operation`

以上函数调用后,会返回一个引用,可以通过该引用关闭弹窗。

  1. constructor(modal: ModalService) {
  2. const ref: ModalRef = modal.alert();
  3. ref.close(); // 或 ref.destroy(); 将直接销毁对话框
  4. }

相关的类型定义

ModalService其他的方法/属性

方法/属性描述类型
openModals打开状态的Modal集合ModalRef[]
afterAllClose所有的Modals关闭后的回调Observable&lt;void&gt;
closeAll()关闭所有的ModalsFunction

ModalRef

ModalRef 对象用于控制对话框以及进行内容间的通信

通过服务方式 ModalService.xxx() 创建的对话框,都会返回一个 ModalRef 对象,该对象具有以下方法:

方法/参数说明
afterOpen同afterOpen,但类型为Observable<void>
afterClose同afterClose,但类型为Observable<result:any>
close(result: any) => void关闭(隐藏)对话框。注:当用于以服务方式创建的对话框,此方法将直接 销毁 对话框(同destroy方法)
destroy(result: any) => void销毁对话框。注:仅用于服务方式创建的对话框(非服务方式创建的对话框,此方法只会隐藏对话框)
getContentComponent() => Component获取对话框内容中Content的Component实例instance。注:当对话框还未初始化完毕(ngOnInit未执行)时,此函数将返回undefined
triggerOk() => void手动触发onClose
triggerCancel() => void手动触发cancel