Drawer抽屉
抽屉从父窗体边缘滑入,覆盖住部分父窗体内容。用户在抽屉内操作时不必离开当前任务,操作完成后,可以平滑地回到到原任务。
何时使用
- 当需要一个附加的面板来控制父窗体内容,这个面板在需要时呼出。比如,控制界面展示样式,往界面中添加内容。
- 当需要在当前任务流中插入临时任务,创建或预览附加内容。比如展示协议条款,创建子对象。
import { NzDrawerModule } from 'ng-zorro-antd/drawer';
代码演示
基础抽屉
基础抽屉,点击触发按钮抽屉从右滑出,点击遮罩区关闭
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-drawer-basic-right',
template: `
<button nz-button nzType="primary" (click)="open()">Open</button>
<nz-drawer [nzClosable]="false" [nzVisible]="visible" nzPlacement="right" nzTitle="Basic Drawer" (nzOnClose)="close()">
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</nz-drawer>
`
})
export class NzDemoDrawerBasicRightComponent {
visible = false;
open(): void {
this.visible = true;
}
close(): void {
this.visible = false;
}
}
对象编辑
用于承载编辑相关操作,需要点击关闭按钮关闭。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-drawer-from-drawer',
template: `
<button nz-button nzType="primary" (click)="open()">Create</button>
<nz-drawer
[nzBodyStyle]="{ overflow: 'auto' }"
[nzMaskClosable]="false"
[nzWidth]="720"
[nzVisible]="visible"
nzTitle="Create"
[nzFooter]="footerTpl"
(nzOnClose)="close()"
>
<form nz-form>
<div nz-row [nzGutter]="8">
<div nz-col nzSpan="12">
<nz-form-item>
<nz-form-label>Name</nz-form-label>
<nz-form-control>
<input nz-input placeholder="please enter user name" />
</nz-form-control>
</nz-form-item>
</div>
<div nz-col nzSpan="12">
<nz-form-item>
<nz-form-label>Url</nz-form-label>
<nz-form-control>
<nz-input-group nzAddOnBefore="http://" nzAddOnAfter=".com">
<input type="text" nz-input placeholder="please enter url" />
</nz-input-group>
</nz-form-control>
</nz-form-item>
</div>
</div>
<div nz-row [nzGutter]="8">
<div nz-col nzSpan="12">
<nz-form-item>
<nz-form-label>Owner</nz-form-label>
<nz-form-control>
<nz-select nzPlaceHolder="Please select an owner"></nz-select>
</nz-form-control>
</nz-form-item>
</div>
<div nz-col nzSpan="12">
<nz-form-item>
<nz-form-label>Type</nz-form-label>
<nz-form-control>
<nz-select nzPlaceHolder="Please choose the type"></nz-select>
</nz-form-control>
</nz-form-item>
</div>
</div>
<div nz-row [nzGutter]="8">
<div nz-col nzSpan="12">
<nz-form-item>
<nz-form-label>Approver</nz-form-label>
<nz-form-control>
<nz-select nzPlaceHolder="Please choose the approver"></nz-select>
</nz-form-control>
</nz-form-item>
</div>
<div nz-col nzSpan="12">
<nz-form-item>
<nz-form-label>DateTime</nz-form-label>
<nz-form-control>
<nz-range-picker></nz-range-picker>
</nz-form-control>
</nz-form-item>
</div>
</div>
<div nz-row [nzGutter]="8">
<div nz-col nzSpan="24">
<nz-form-item>
<nz-form-label>Description</nz-form-label>
<nz-form-control>
<textarea nz-input placeholder="please enter url description" [nzAutosize]="{ minRows: 4, maxRows: 4 }"></textarea>
</nz-form-control>
</nz-form-item>
</div>
</div>
<ng-template #footerTpl>
<div style="float: right">
<button nz-button style="margin-right: 8px;" (click)="close()">Cancel</button>
<button nz-button nzType="primary" (click)="close()">Submit</button>
</div>
</ng-template>
</form>
</nz-drawer>
`
})
export class NzDemoDrawerFromDrawerComponent {
visible = false;
open(): void {
this.visible = true;
}
close(): void {
this.visible = false;
}
}
多层抽屉
在抽屉内打开新的抽屉,用以解决多分支任务的复杂状况。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-drawer-multi-level-drawer',
template: `
<button nz-button nzType="primary" (click)="open()">New Cookbook</button>
<nz-drawer
[nzClosable]="false"
[nzOffsetX]="childrenVisible ? 180 : 0"
[nzWidth]="320"
[nzVisible]="visible"
nzTitle="Cookbook"
(nzOnClose)="close()"
>
<form nz-form>
<div nz-row>
<div nz-col nzSpan="24">
<nz-form-item>
<nz-form-label>Name</nz-form-label>
<nz-form-control>
<input nz-input placeholder="please enter cookbook name" />
</nz-form-control>
</nz-form-item>
</div>
</div>
<div nz-row>
<div nz-col nzSpan="24">
<nz-form-item>
<nz-form-label>Food</nz-form-label>
<nz-form-control>
<nz-tag>potato</nz-tag>
<nz-tag>eggplant</nz-tag>
<nz-tag (click)="openChildren()">+</nz-tag>
</nz-form-control>
</nz-form-item>
</div>
</div>
</form>
<nz-drawer [nzClosable]="false" [nzVisible]="childrenVisible" nzTitle="Food" (nzOnClose)="closeChildren()">
<nz-list [nzDataSource]="vegetables" [nzRenderItem]="item">
<ng-template #item let-item>
<nz-list-item [nzContent]="item"></nz-list-item>
</ng-template>
</nz-list>
</nz-drawer>
</nz-drawer>
`
})
export class NzDemoDrawerMultiLevelDrawerComponent {
visible = false;
childrenVisible = false;
vegetables = ['asparagus', 'bamboo', 'potato', 'carrot', 'cilantro', 'potato', 'eggplant'];
open(): void {
this.visible = true;
}
close(): void {
this.visible = false;
}
openChildren(): void {
this.childrenVisible = true;
}
closeChildren(): void {
this.childrenVisible = false;
}
}
自定义位置
自定义位置,点击触发按钮抽屉从相应的位置滑出,点击遮罩区关闭
import { Component } from '@angular/core';
import { NzDrawerPlacement } from 'ng-zorro-antd/drawer';
@Component({
selector: 'nz-demo-drawer-placement',
template: `
<nz-radio-group [(ngModel)]="placement">
<label nz-radio nzValue="top">top</label>
<label nz-radio nzValue="right">right</label>
<label nz-radio nzValue="bottom">bottom</label>
<label nz-radio nzValue="left">left</label>
</nz-radio-group>
<button nz-button nzType="primary" (click)="open()">Open</button>
<nz-drawer [nzClosable]="false" [nzVisible]="visible" [nzPlacement]="placement" nzTitle="Basic Drawer" (nzOnClose)="close()">
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</nz-drawer>
`
})
export class NzDemoDrawerPlacementComponent {
visible = false;
placement: NzDrawerPlacement = 'left';
open(): void {
this.visible = true;
}
close(): void {
this.visible = false;
}
}
信息预览抽屉
需要快速预览对象概要时使用,点击遮罩区关闭。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-drawer-user-profile',
template: `
<nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'">
<ng-template #item let-item>
<nz-list-item [nzActions]="[viewAction]">
<ng-template #viewAction>
<a (click)="open()">View Profile</a>
</ng-template>
<nz-list-item-meta
[nzTitle]="nzTitle"
nzAvatar="https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png"
nzDescription="Progresser AFX"
>
<ng-template #nzTitle>
<a href="https://ng.ant.design">{{ item.name }}</a>
</ng-template>
</nz-list-item-meta>
</nz-list-item>
</ng-template>
</nz-list>
<nz-drawer [nzVisible]="visible" [nzWidth]="640" [nzClosable]="false" (nzOnClose)="close()">
<p class="title" style=" margin-bottom: 24px;">User Profile</p>
<nz-descriptions [nzColumn]="2" nzTitle="Personal">
<nz-descriptions-item nzTitle="Full Name" [nzSpan]="1">Lily</nz-descriptions-item>
<nz-descriptions-item nzTitle="Account" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
<nz-descriptions-item nzTitle="City" [nzSpan]="1">HangZhou</nz-descriptions-item>
<nz-descriptions-item nzTitle="Country" [nzSpan]="1">China🇨🇳</nz-descriptions-item>
<nz-descriptions-item nzTitle="Birthday" [nzSpan]="1">February 2,1900</nz-descriptions-item>
<nz-descriptions-item nzTitle="Website" [nzSpan]="1"> - </nz-descriptions-item>
<nz-descriptions-item nzTitle="Message" [nzSpan]="2">
Make things as simple as possible but no simpler.
</nz-descriptions-item>
</nz-descriptions>
<nz-divider></nz-divider>
<nz-descriptions [nzColumn]="2" nzTitle="Company">
<nz-descriptions-item nzTitle="Position" [nzSpan]="1">Programmer</nz-descriptions-item>
<nz-descriptions-item nzTitle="Responsibilities" [nzSpan]="1">Coding</nz-descriptions-item>
<nz-descriptions-item nzTitle="Department" [nzSpan]="1">AFX</nz-descriptions-item>
<nz-descriptions-item nzTitle="Supervisor" [nzSpan]="1">Lin</nz-descriptions-item>
<nz-descriptions-item nzTitle="Skills" [nzSpan]="2">
C / C + +, data structures, software engineering, operating systems, computer networks, databases, compiler theory, computer
architecture, Microcomputer Principle and Interface Technology, Computer English, Java, ASP, etc.
</nz-descriptions-item>
</nz-descriptions>
<nz-divider></nz-divider>
<nz-descriptions [nzColumn]="2" nzTitle="Contacts">
<nz-descriptions-item nzTitle="Email" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
<nz-descriptions-item nzTitle="Phone Number" [nzSpan]="1">+86 181 0000 0000</nz-descriptions-item>
<nz-descriptions-item nzTitle="Github" [nzSpan]="2">
<a href="https://github.com/NG-ZORRO/ng-zorro-antd" target="_blank">github.com/NG-ZORRO/ng-zorro-antd</a>
</nz-descriptions-item>
</nz-descriptions>
</nz-drawer>
`,
styles: [
`
.title {
font-size: 16px;
color: rgba(0, 0, 0, 0.85);
line-height: 24px;
display: block;
margin-bottom: 16px;
}
.item-wrap {
font-size: 14px;
line-height: 22px;
margin-bottom: 7px;
color: rgba(0, 0, 0, 0.65);
}
.label {
margin-right: 8px;
display: inline-block;
color: rgba(0, 0, 0, 0.85);
}
`
]
})
export class NzDemoDrawerUserProfileComponent {
data = [
{
name: 'Lily'
},
{
name: 'Lily'
}
];
visible = false;
open(): void {
this.visible = true;
}
close(): void {
this.visible = false;
}
}
服务方式创建
Drawer 的 service 用法,示例中演示了用户自定义模板、自定义component。
/* declarations: NzDrawerCustomComponent */
import { Component, Input, TemplateRef, ViewChild } from '@angular/core';
import { NzDrawerRef, NzDrawerService } from 'ng-zorro-antd/drawer';
@Component({
selector: 'nz-demo-drawer-service',
template: `
<ng-template #drawerTemplate let-data let-drawerRef="drawerRef">
value: {{ data?.value }}
<br />
<button nz-button nzType="primary" (click)="drawerRef.close()">close</button>
</ng-template>
<div nz-form>
<nz-form-item>
<input nz-input [(ngModel)]="value" />
</nz-form-item>
</div>
<button nz-button nzType="primary" (click)="openTemplate()">Use Template</button>
<button nz-button nzType="primary" (click)="openComponent()">Use Component</button>
`
})
export class NzDemoDrawerServiceComponent {
@ViewChild('drawerTemplate', { static: false }) drawerTemplate?: TemplateRef<{
$implicit: { value: string };
drawerRef: NzDrawerRef<string>;
}>;
value = 'ng';
constructor(private drawerService: NzDrawerService) {}
openTemplate(): void {
const drawerRef = this.drawerService.create({
nzTitle: 'Template',
nzFooter: 'Footer',
nzContent: this.drawerTemplate,
nzContentParams: {
value: this.value
}
});
drawerRef.afterOpen.subscribe(() => {
console.log('Drawer(Template) open');
});
drawerRef.afterClose.subscribe(() => {
console.log('Drawer(Template) close');
});
}
openComponent(): void {
const drawerRef = this.drawerService.create<NzDrawerCustomComponent, { value: string }, string>({
nzTitle: 'Component',
nzContent: NzDrawerCustomComponent,
nzContentParams: {
value: this.value
}
});
drawerRef.afterOpen.subscribe(() => {
console.log('Drawer(Component) open');
});
drawerRef.afterClose.subscribe(data => {
console.log(data);
if (typeof data === 'string') {
this.value = data;
}
});
}
}
@Component({
selector: 'nz-drawer-custom-component',
template: `
<div>
<input nz-input [(ngModel)]="value" />
<nz-divider></nz-divider>
<button nzType="primary" (click)="close()" nz-button>Confirm</button>
</div>
`
})
export class NzDrawerCustomComponent {
@Input() value = '';
constructor(private drawerRef: NzDrawerRef<string>) {}
close(): void {
this.drawerRef.close(this.value);
}
}
API
nz-drawercomponent
参数 | 说明 | 类型 | 默认值 | 全局配置 |
---|---|---|---|---|
[nzClosable] | 是否显示右上角的关闭按钮 | boolean | true | |
[nzCloseIcon] | 自定义关闭图标 | string | TemplateRef<void> | null | ‘close’ | |
[nzMaskClosable] | 点击蒙层是否允许关闭 | boolean | true | ✅ |
[nzMask] | 是否展示遮罩 | boolean | true | ✅ |
[nzCloseOnNavigation] | 导航历史变化时是否关闭抽屉组件 | boolean | true | ✅ |
[nzMaskStyle] | 遮罩样式 | object | {} | |
[nzKeyboard] | 是否支持键盘 esc 关闭 | boolean | true | |
[nzBodyStyle] | Drawer body 样式 | object | {} | |
[nzTitle] | 标题 | string | TemplateRef<void> | - | |
[nzFooter] | 抽屉的页脚 | string | TemplateRef<void> | - | |
[nzVisible] | Drawer 是否可见 | boolean | - | |
[nzPlacement] | 抽屉的方向 | ‘top’ | ‘right’ | ‘bottom’ | ‘left’ | ‘right’ | |
[nzWidth] | 宽度, 只在方向为 ‘right’ 或‘left’ 时生效 | number | string | 256 | |
[nzHeight] | 高度, 只在方向为 ‘top’ 或‘bottom’ 时生效 | number | string | 256 | |
[nzOffsetX] | x 坐标移量(px), 只在方向为 ‘right’ 或‘left’ 时生效 | number | 0 | |
[nzOffsetY] | y 坐标移量(px), 高度, 只在方向为 ‘top’ 或‘bottom’ 时生效 | number | 0 | |
[nzWrapClassName] | 对话框外层容器的类名 | string | - | |
[nzZIndex] | 设置 Drawer 的 z-index | number | 1000 | |
(nzOnClose) | 点击遮罩层或右上角叉的回调 | EventEmitter<MouseEvent> | - |
NzDrawerServiceservice
方法名 | 说明 | 参数 | 返回 |
---|---|---|---|
create<T, D, R> | 创建并打开一个 Drawer | NzDrawerOptions<T, D> | NzDrawerRef<T, R> |
NzDrawerOptions
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
nzContent | Drawer body 的内容 | TemplateRef<{ $implicit: D, drawerRef: NzDrawerRef }> | Type<T> | - |
nzContentParams | 内容组件的输入参数 / Template的 context | D | - |
nzOnCancel | 点击遮罩层或右上角叉时执行,该函数可返回 promise 待执行完毕或 promise 结束时,将自动关闭对话框(返回false可阻止关闭) | () => Promise<any> | - |
nzClosable | 是否显示右上角的关闭按钮 | boolean | true |
nzCloseIcon | 自定义关闭图标 | string | TemplateRef<void> | null | ‘close’ |
nzMaskClosable | 点击蒙层是否允许关闭 | boolean | true |
nzMask | 是否展示遮罩 | boolean | true |
nzCloseOnNavigation | 导航历史变化时是否关闭抽屉组件 | boolean | true |
nzKeyboard | 是否支持键盘esc关闭 | boolean | true |
nzMaskStyle | 遮罩样式 | object | {} |
nzBodyStyle | Modal body 样式 | object | {} |
nzTitle | 标题 | string | TemplateRef<void> | - |
nzFooter | 页脚 | string | TemplateRef<void> | - |
nzWidth | 宽度 | number | string | 256 |
nzHeight | 高度, 只在方向为 ‘top’ 或‘bottom’ 时生效 | number | string | 256 |
nzWrapClassName | 对话框外层容器的类名 | string | - |
nzZIndex | 设置 Drawer 的 z-index | number | 1000 |
nzPlacement | 抽屉的方向 | ‘top’ | ‘right’ | ‘bottom’ | ‘left’ | ‘right’ |
nzOffsetX | x 坐标移量(px) | number | 0 |
nzOffsetY | y 坐标移量(px), 高度, 只在方向为 ‘top’ 或‘bottom’ 时生效 | number | 0 |
NzDrawerRef
方法
名称 | 说明 | 类型 |
---|---|---|
close | 关闭 Drawer | (result?: R) => void |
open | 打开 Drawer | () => void |
getContentComponent | 返回 nzContent 为组件时的组件实例 | () => T | null |
属性
名称 | 说明 | 类型 |
---|---|---|
afterOpen | 打开之后的回调 | Observable<void> |
afterClose | 关闭之后的回调 | Observable<R> |
nzClosable | 是否显示右上角的关闭按钮 | boolean |
nzCloseIcon | 自定义关闭图标 | string | TemplateRef<void> | null |
nzMaskClosable | 点击蒙层是否允许关闭 | boolean |
nzMask | 是否展示遮罩 | boolean |
nzMaskStyle | 遮罩样式 | object |
nzKeyboard | 是否支持键盘esc关闭 | boolean |
nzBodyStyle | Modal body 样式 | object |
nzTitle | 标题 | string | TemplateRef<void> |
nzWidth | 宽度 | number | string |
nzHeight | 高度, 只在方向为 ‘top’ 或‘bottom’ 时生效 | number | string |
nzWrapClassName | 对话框外层容器的类名 | string |
nzZIndex | 设置 Drawer 的 z-index | number |
nzPlacement | 抽屉的方向 | ‘top’ | ‘right’ | ‘bottom’ | ‘left’ |
nzOffsetX | x 坐标移量(px) | number |
nzOffsetY | y 坐标移量(px), 高度, 只在方向为 ‘top’ 或‘bottom’ 时生效 | number |