Modal对话框
用作显示系统的重要信息,并请求用户进行操作反馈,eg:删除某个重要内容时,弹出 Modal 进行二次确认。
规则
- 尽可能少用。Modal 会打断用户操作,只用在重要的时候。
- 标题应该简明,不能超过 1 行;描述内容应该简明、完整,一般不多于 2 行。
- 操作按钮最多到 3 个(竖排),一般为 1-2 个(横排);3 个以上建议使用组件 ActionSheet 来完成。
- 一般将用户最可能点击的按钮,放在右侧。另外,取消按钮应当始终放在左侧。
代码演示
基本用法
最简单的用法。
import { Component } from '@angular/core';
import { ModalService } from 'ng-zorro-antd-mobile';
@Component({
selector: 'demo-modal-basic',
template: `
<WingBlank>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showModal('modal1')">text only</div>
<WhiteSpace></WhiteSpace>
<Modal [(ngModel)]="this.state.modal1" [transparent]="true" [title]="'Title'" [footer]="footer">
<div [ngStyle]="{ height: 100, overflow: 'scroll' }">
scoll content... <br />
scoll content... <br />
scoll content... <br />
scoll content... <br />
scoll content... <br />
scoll content... <br />
</div>
</Modal>
<div Button (onClick)="showModal('modal2')">popup</div>
<WhiteSpace></WhiteSpace>
<Modal [(ngModel)]="this.state.modal2" [popup]="true" [animationType]="'slide-up'" (onClose)="onClose('modal2')">
<List [renderHeader]="renderHeader" [className]="'popup-list'">
<ListItem>股票名称</ListItem>
<ListItem>股票代码</ListItem>
<ListItem>买入价格</ListItem>
<ListItem> <div Button [type]="'primary'" (onClick)="onClose('modal2')">买入</div> </ListItem>
</List>
</Modal>
<div Button (onClick)="showModal('modal3')">maskClosable</div>
<WhiteSpace></WhiteSpace>
<Modal
[(ngModel)]="this.state.modal3"
[transparent]="true"
[title]="'Title'"
[maskClosable]="true"
(onClose)="onClose('modal3')"
>
<div [ngStyle]="{ height: 100, overflow: 'scroll' }">
scoll content... <br />
scoll content... <br />
scoll content... <br />
</div>
</Modal>
<div Button (onClick)="showModal('modal4')">closable</div>
<WhiteSpace></WhiteSpace>
<Modal
[(ngModel)]="this.state.modal4"
[transparent]="true"
[title]="'Title'"
[closable]="true"
(onClose)="onClose('modal4')"
>
<div [ngStyle]="{ height: 100, overflow: 'scroll' }">
scoll content... <br />
scoll content... <br />
scoll content... <br />
</div>
</Modal>
</WingBlank>
`,
styles: [
`
.popup-list .am-list-body {
height: 210px;
overflow: auto;
}
`
]
})
export class DemoModalBasicComponent {
state = {
modal1: false,
modal2: false,
modal3: false,
modal4: false
};
footer = [
{
text: 'Ok',
onPress: () => {
console.log('ok');
this.onClose('modal1');
}
}
];
footer2 = [
{
text: 'Ok',
onPress: () => {
console.log('ok');
this.onClose('modal2');
}
}
];
constructor() {}
modelChange(event) {
console.log('asdfasdf', event);
}
onClose(key) {
this.state[key] = false;
}
showModal(key) {
this.state[key] = true;
}
renderHeader() {
return '委托买入';
}
}
警告弹窗
包含无按钮, 确认框, 多按钮情况。
import { Component } from '@angular/core';
import { ModalService, ToastService, ModalRef } from 'ng-zorro-antd-mobile';
@Component({
selector: 'demo-modal-alert',
template: `
<WingBlank>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showAlert()">customized buttons</div>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showAlertMuchButtons(message)">More than two buttons</div>
<WhiteSpace></WhiteSpace>
<ng-template #message>
<div>More than two buttons</div>
</ng-template>
<div Button (onClick)="showPromise()">promise</div>
<WhiteSpace></WhiteSpace>
</WingBlank>
`
})
export class DemoModalAlertComponent {
constructor(private _modal: ModalService, private _toast: ToastService) {}
showAlert() {
this._modal.alert('Delete', 'Are you sure ?', [
{ text: 'Cancel', onPress: () => console.log('cancel') },
{ text: 'OK', onPress: () => console.log('ok') }
]);
}
showAlertMuchButtons(message) {
this._modal.alert('Much Buttons', message, [
{ text: 'Button1', onPress: () => console.log('第0个按钮被点击了') },
{ text: 'Button2', onPress: () => console.log('第1个按钮被点击了') },
{ text: 'Button2', onPress: () => console.log('第2个按钮被点击了') }
]);
}
showPromise() {
this._modal.alert('Delete', 'Are you sure???', [
{ text: 'Cancel', onPress: () => console.log('cancel') },
{
text: 'Ok',
onPress: () =>
new Promise(resolve => {
this._toast.info('onPress Promise', 1000);
setTimeout(resolve, 1000);
}),
style: {
color: '#ffffff',
background: '#00ff00'
}
}
]);
}
}
输入弹窗
包含输入普通文字, 密码, 登录信息样式。
import { Component } from '@angular/core';
import { ModalService, ToastService } from 'ng-zorro-antd-mobile';
@Component({
selector: 'demo-modal-prompt',
template: `
<WingBlank>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showPromptPromise()">promise</div>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showPromptDefault()">defaultValue</div>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showSecure()">secure-text</div>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showCustom()">custom buttons</div>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showLogin()">login-password</div>
<WhiteSpace></WhiteSpace>
</WingBlank>
`
})
export class DemoModalPromptComponent {
constructor(private _modal: ModalService, private _toast: ToastService) {}
showPromptPromise() {
this._modal.prompt(
'input name',
'please input your name',
[
{
text: 'Close',
onPress: value =>
new Promise(resolve => {
this._toast.info('onPress promise resolve', 1000);
setTimeout(() => {
resolve();
console.log(`value:${value}`);
}, 1000);
})
},
{
text: 'Hold on',
onPress: value =>
new Promise((resolve, reject) => {
this._toast.info('onPress promise reject', 1000);
setTimeout(() => {
// reject();
console.log(`value:${value}`);
}, 1000);
})
}
],
'default',
null,
['input your name']
);
}
showPromptDefault() {
this._modal.prompt(
'defaultValue',
'defaultValue for prompt',
[{ text: 'Cancel' }, { text: 'Submit', onPress: value => console.log(`输入的内容:${value}`) }],
'default',
['100']
);
}
showSecure() {
this._modal.prompt('Password', 'Password Message', password => console.log(`password: ${password}`), 'secure-text');
}
showCustom() {
this._modal.prompt(
'Password',
'You can custom buttons',
[{ text: '取消' }, { text: '提交', onPress: password => console.log(`密码为:${password}`) }],
'secure-text'
);
}
showLogin() {
this._modal.prompt(
'Login',
'Please input login information',
(login, password) => console.log(`login: ${login}, password: ${password}`),
'login-password',
['default', '123456'],
['Please input name', 'Please input password']
);
}
}
操作弹窗
操作对话框。
import { Component } from '@angular/core';
import { ModalService } from 'ng-zorro-antd-mobile';
@Component({
selector: 'demo-modal-operation',
template: `
<WingBlank>
<WhiteSpace></WhiteSpace>
<div Button (onClick)="showOpeartion()">operation</div>
<WhiteSpace></WhiteSpace>
</WingBlank>
`
})
export class DemoModalOperationComponent {
constructor(private _modal: ModalService) {}
showOpeartion() {
this._modal.operation([
{ text: '标为未读', onPress: () => console.log('标为未读被点击了') },
{ text: '置顶聊天', onPress: () => console.log('置顶聊天被点击了') }
]);
}
}
API
Modal
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[ngModel] | 对话框是否可见 | boolean | false |
[closable] | 是否显示关闭按钮 | boolean | false |
[maskClosable] | 点击蒙层是否允许关闭 | boolean | true |
[transparent] | 是否背景透明 | boolean | false |
[popup] | 是否弹窗模式 | boolean | false |
[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] | defaultValue | string[] | - |
[placeholders] | placeholders | string[] | - |
[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`
以上函数调用后,会返回一个引用,可以通过该引用关闭弹窗。
constructor(modal: ModalService) {
const ref: ModalRef = modal.alert();
ref.close(); // 或 ref.destroy(); 将直接销毁对话框
}
相关的类型定义
ModalService其他的方法/属性
方法/属性 | 描述 | 类型 |
---|---|---|
openModals | 打开状态的Modal集合 | ModalRef[] |
afterAllClose | 所有的Modals关闭后的回调 | Observable<void> |
closeAll() | 关闭所有的Modals | Function |
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 |