• Modal对话框
    • 规则
  • 代码演示
  • API
    • Modal
    • ModalSerivce.alert(title, message, actions?, platform?)
    • ModalSerivce.prompt(title, message, callbackOrActions, type?, defaultValue?, placeholders?, platform?)
    • ModalSerivce.operation(actions?, platform?)

    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 [(ngModel)]="this.state.modal3" [transparent]="true" [title]="'Title'" [maskClosable]="true" (onClose)="onClose('modal3')">
    33. <div [ngStyle]="{ height: 100, overflow: 'scroll' }">
    34. scoll content... <br />
    35. scoll content... <br />
    36. scoll content... <br />
    37. </div>
    38. </Modal>
    39. <div Button (onClick)="showModal('modal4')">closable</div>
    40. <WhiteSpace></WhiteSpace>
    41. <Modal [(ngModel)]="this.state.modal4" [transparent]="true" [title]="'Title'" [closable]="true" (onClose)="onClose('modal4')">
    42. <div [ngStyle]="{ height: 100, overflow: 'scroll' }">
    43. scoll content... <br />
    44. scoll content... <br />
    45. scoll content... <br />
    46. </div>
    47. </Modal>
    48. </WingBlank>
    49. `,
    50. styles: [
    51. `
    52. .popup-list .am-list-body {
    53. height: 210px;
    54. overflow: auto;
    55. }
    56. `
    57. ]
    58. })
    59. export class DemoModalBasicComponent {
    60. state = {
    61. modal1: false,
    62. modal2: false,
    63. modal3: false,
    64. modal4: false
    65. };
    66. footer = [
    67. {
    68. text: 'Ok',
    69. onPress: () => {
    70. console.log('ok');
    71. this.onClose('modal1');
    72. }
    73. }
    74. ];
    75. footer2 = [
    76. {
    77. text: 'Ok',
    78. onPress: () => {
    79. console.log('ok');
    80. this.onClose('modal2');
    81. }
    82. }
    83. ];
    84. constructor(private _modal: ModalService) {}
    85. modelChange(event) {
    86. console.log('asdfasdf', event);
    87. }
    88. onClose(key) {
    89. this.state[key] = false;
    90. }
    91. showModal(key) {
    92. this.state[key] = true;
    93. }
    94. renderHeader() {
    95. return '委托买入';
    96. }
    97. }

    警告弹窗

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

    1. import { Component } from '@angular/core';
    2. import { ModalService, ToastService } 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. ModalService.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. ModalService.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. ModalService.alert('Delete', 'Are you sure???', [
    37. { text: 'Cancel', onPress: () => console.log('cancel') },
    38. {
    39. text: 'Ok',
    40. onPress: () =>
    41. new Promise(resolve => {
    42. ToastService.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. ModalService.prompt(
    25. 'input name',
    26. 'please input your name',
    27. [
    28. {
    29. text: 'Close',
    30. onPress: value =>
    31. new Promise(resolve => {
    32. ToastService.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. ToastService.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. ModalService.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. ModalService.prompt('Password', 'Password Message', password => console.log(`password: ${password}`), 'secure-text');
    67. }
    68. showCustom() {
    69. ModalService.prompt(
    70. 'Password',
    71. 'You can custom buttons',
    72. [{ text: '取消' }, { text: '提交', onPress: password => console.log(`密码为:${password}`) }],
    73. 'secure-text'
    74. );
    75. }
    76. showLogin() {
    77. ModalService.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. ModalService.operation([
    17. { text: '标为未读', onPress: () => console.log('标为未读被点击了') },
    18. { text: '置顶聊天', onPress: () => console.log('置顶聊天被点击了') }
    19. ]);
    20. }
    21. }

    Modal 对话框 - 图1

    API

    Modal

    属性说明类型默认值
    visible对话框是否可见Booleanfalse
    closable是否显示关闭按钮Booleanfalse
    maskClosable点击蒙层是否允许关闭Booleantrue
    onClose点击 x 或 mask 回调(): void
    transparent是否背景透明Booleanfalse
    popup是否弹窗模式Booleanfalse
    animationType可选: 'slide-down/up' / 'fade' / 'slide'Stringfade
    title标题TemplateRef
    footer底部内容Array [{text, onPress}][]
    platform (Web Only)设定组件的平台特有样式, 可选值为 android, ios, 默认为 iosStringios'

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

    属性说明类型默认值
    title标题String 或 TemplateRef
    message提示信息String 或 TemplateRef
    actions按钮组, [{text, onPress, style}]Array
    platform设定组件的平台特有样式, 可选值为 android, ios, 默认为 iosString'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按钮组 [{text, onPress}] 或回调函数Array or Function
    typeprompt 的样式String (default, secure-text, login-password)default
    defaultValue['', '']String[]-
    placeholders['', '']String[]-
    platform设定组件的平台特有样式, 可选值为 android, ios, 默认为 iosString'ios'

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

    ModalSerivce.operation(actions?, platform?)

    属性说明类型默认值
    actions按钮组, [{text, onPress, style}]Array
    platform设定组件的平台特有样式, 可选值为 android, ios, 默认为 iosString'ios'

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