Drawer抽屉

抽屉从父窗体边缘滑入,覆盖住部分父窗体内容。用户在抽屉内操作时不必离开当前任务,操作完成后,可以平滑地回到到原任务。

何时使用

  • 当需要一个附加的面板来控制父窗体内容,这个面板在需要时呼出。比如,控制界面展示样式,往界面中添加内容。
  • 当需要在当前任务流中插入临时任务,创建或预览附加内容。比如展示协议条款,创建子对象。
  1. import { NzDrawerModule } from 'ng-zorro-antd/drawer';

代码演示

Drawer抽屉 - 图1

基础抽屉

基础抽屉,点击触发按钮抽屉从右滑出,点击遮罩区关闭

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-drawer-basic-right',
  4. template: `
  5. <button nz-button nzType="primary" (click)="open()">Open</button>
  6. <nz-drawer [nzClosable]="false" [nzVisible]="visible" nzPlacement="right" nzTitle="Basic Drawer" (nzOnClose)="close()">
  7. <p>Some contents...</p>
  8. <p>Some contents...</p>
  9. <p>Some contents...</p>
  10. </nz-drawer>
  11. `
  12. })
  13. export class NzDemoDrawerBasicRightComponent {
  14. visible = false;
  15. open(): void {
  16. this.visible = true;
  17. }
  18. close(): void {
  19. this.visible = false;
  20. }
  21. }

Drawer抽屉 - 图2

对象编辑

用于承载编辑相关操作,需要点击关闭按钮关闭。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-drawer-from-drawer',
  4. template: `
  5. <button nz-button nzType="primary" (click)="open()">Create</button>
  6. <nz-drawer
  7. [nzBodyStyle]="{ height: 'calc(100% - 55px)', overflow: 'auto', 'padding-bottom': '53px' }"
  8. [nzMaskClosable]="false"
  9. [nzWidth]="720"
  10. [nzVisible]="visible"
  11. nzTitle="Create"
  12. (nzOnClose)="close()"
  13. >
  14. <form nz-form>
  15. <div nz-row [nzGutter]="8">
  16. <div nz-col nzSpan="12">
  17. <nz-form-item>
  18. <nz-form-label>Name</nz-form-label>
  19. <nz-form-control>
  20. <input nz-input placeholder="please enter user name" />
  21. </nz-form-control>
  22. </nz-form-item>
  23. </div>
  24. <div nz-col nzSpan="12">
  25. <nz-form-item>
  26. <nz-form-label>Url</nz-form-label>
  27. <nz-form-control>
  28. <nz-input-group nzAddOnBefore="http://" nzAddOnAfter=".com">
  29. <input type="text" nz-input placeholder="please enter url" />
  30. </nz-input-group>
  31. </nz-form-control>
  32. </nz-form-item>
  33. </div>
  34. </div>
  35. <div nz-row [nzGutter]="8">
  36. <div nz-col nzSpan="12">
  37. <nz-form-item>
  38. <nz-form-label>Owner</nz-form-label>
  39. <nz-form-control>
  40. <nz-select nzPlaceHolder="Please select an owner"></nz-select>
  41. </nz-form-control>
  42. </nz-form-item>
  43. </div>
  44. <div nz-col nzSpan="12">
  45. <nz-form-item>
  46. <nz-form-label>Type</nz-form-label>
  47. <nz-form-control>
  48. <nz-select nzPlaceHolder="Please choose the type"></nz-select>
  49. </nz-form-control>
  50. </nz-form-item>
  51. </div>
  52. </div>
  53. <div nz-row [nzGutter]="8">
  54. <div nz-col nzSpan="12">
  55. <nz-form-item>
  56. <nz-form-label>Approver</nz-form-label>
  57. <nz-form-control>
  58. <nz-select nzPlaceHolder="Please choose the approver"></nz-select>
  59. </nz-form-control>
  60. </nz-form-item>
  61. </div>
  62. <div nz-col nzSpan="12">
  63. <nz-form-item>
  64. <nz-form-label>DateTime</nz-form-label>
  65. <nz-form-control>
  66. <nz-range-picker></nz-range-picker>
  67. </nz-form-control>
  68. </nz-form-item>
  69. </div>
  70. </div>
  71. <div nz-row [nzGutter]="8">
  72. <div nz-col nzSpan="24">
  73. <nz-form-item>
  74. <nz-form-label>Description</nz-form-label>
  75. <nz-form-control>
  76. <textarea nz-input placeholder="please enter url description" [nzAutosize]="{ minRows: 4, maxRows: 4 }"></textarea>
  77. </nz-form-control>
  78. </nz-form-item>
  79. </div>
  80. </div>
  81. </form>
  82. </nz-drawer>
  83. `
  84. })
  85. export class NzDemoDrawerFromDrawerComponent {
  86. visible = false;
  87. open(): void {
  88. this.visible = true;
  89. }
  90. close(): void {
  91. this.visible = false;
  92. }
  93. }

Drawer抽屉 - 图3

多层抽屉

在抽屉内打开新的抽屉,用以解决多分支任务的复杂状况。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-drawer-multi-level-drawer',
  4. template: `
  5. <button nz-button nzType="primary" (click)="open()">New Cookbook</button>
  6. <nz-drawer
  7. [nzClosable]="false"
  8. [nzOffsetX]="childrenVisible ? 180 : 0"
  9. [nzWidth]="320"
  10. [nzVisible]="visible"
  11. nzTitle="Cookbook"
  12. (nzOnClose)="close()"
  13. >
  14. <form nz-form>
  15. <div nz-row>
  16. <div nz-col nzSpan="24">
  17. <nz-form-item>
  18. <nz-form-label>Name</nz-form-label>
  19. <nz-form-control>
  20. <input nz-input placeholder="please enter cookbook name" />
  21. </nz-form-control>
  22. </nz-form-item>
  23. </div>
  24. </div>
  25. <div nz-row>
  26. <div nz-col nzSpan="24">
  27. <nz-form-item>
  28. <nz-form-label>Food</nz-form-label>
  29. <nz-form-control>
  30. <nz-tag>potato</nz-tag>
  31. <nz-tag>eggplant</nz-tag>
  32. <nz-tag (click)="openChildren()">+</nz-tag>
  33. </nz-form-control>
  34. </nz-form-item>
  35. </div>
  36. </div>
  37. </form>
  38. <nz-drawer [nzClosable]="false" [nzVisible]="childrenVisible" nzTitle="Food" (nzOnClose)="closeChildren()">
  39. <nz-list [nzDataSource]="vegetables" [nzRenderItem]="item">
  40. <ng-template #item let-item>
  41. <nz-list-item [nzContent]="item"></nz-list-item>
  42. </ng-template>
  43. </nz-list>
  44. </nz-drawer>
  45. </nz-drawer>
  46. `
  47. })
  48. export class NzDemoDrawerMultiLevelDrawerComponent {
  49. visible = false;
  50. childrenVisible = false;
  51. vegetables = ['asparagus', 'bamboo', 'potato', 'carrot', 'cilantro', 'potato', 'eggplant'];
  52. open(): void {
  53. this.visible = true;
  54. }
  55. close(): void {
  56. this.visible = false;
  57. }
  58. openChildren(): void {
  59. this.childrenVisible = true;
  60. }
  61. closeChildren(): void {
  62. this.childrenVisible = false;
  63. }
  64. }

Drawer抽屉 - 图4

自定义位置

自定义位置,点击触发按钮抽屉从相应的位置滑出,点击遮罩区关闭

  1. import { Component } from '@angular/core';
  2. import { NzDrawerPlacement } from 'ng-zorro-antd/drawer';
  3. @Component({
  4. selector: 'nz-demo-drawer-placement',
  5. template: `
  6. <nz-radio-group [(ngModel)]="placement">
  7. <label nz-radio nzValue="top">top</label>
  8. <label nz-radio nzValue="right">right</label>
  9. <label nz-radio nzValue="bottom">bottom</label>
  10. <label nz-radio nzValue="left">left</label>
  11. </nz-radio-group>
  12. <button nz-button nzType="primary" (click)="open()">Open</button>
  13. <nz-drawer [nzClosable]="false" [nzVisible]="visible" [nzPlacement]="placement" nzTitle="Basic Drawer" (nzOnClose)="close()">
  14. <p>Some contents...</p>
  15. <p>Some contents...</p>
  16. <p>Some contents...</p>
  17. </nz-drawer>
  18. `
  19. })
  20. export class NzDemoDrawerPlacementComponent {
  21. visible = false;
  22. placement: NzDrawerPlacement = 'left';
  23. open(): void {
  24. this.visible = true;
  25. }
  26. close(): void {
  27. this.visible = false;
  28. }
  29. }

Drawer抽屉 - 图5

信息预览抽屉

需要快速预览对象概要时使用,点击遮罩区关闭。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-drawer-user-profile',
  4. template: `
  5. <nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'">
  6. <ng-template #item let-item>
  7. <nz-list-item [nzActions]="[viewAction]">
  8. <ng-template #viewAction>
  9. <a (click)="open()">View Profile</a>
  10. </ng-template>
  11. <nz-list-item-meta
  12. [nzTitle]="nzTitle"
  13. nzAvatar="https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png"
  14. nzDescription="Progresser AFX"
  15. >
  16. <ng-template #nzTitle>
  17. <a href="https://ng.ant.design">{{ item.name }}</a>
  18. </ng-template>
  19. </nz-list-item-meta>
  20. </nz-list-item>
  21. </ng-template>
  22. </nz-list>
  23. <nz-drawer [nzVisible]="visible" [nzWidth]="640" [nzClosable]="false" (nzOnClose)="close()">
  24. <p class="title" style=" margin-bottom: 24px;">User Profile</p>
  25. <nz-descriptions [nzColumn]="2" nzTitle="Personal">
  26. <nz-descriptions-item nzTitle="Full Name" [nzSpan]="1">Lily</nz-descriptions-item>
  27. <nz-descriptions-item nzTitle="Account" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
  28. <nz-descriptions-item nzTitle="City" [nzSpan]="1">HangZhou</nz-descriptions-item>
  29. <nz-descriptions-item nzTitle="Country" [nzSpan]="1">China🇨🇳</nz-descriptions-item>
  30. <nz-descriptions-item nzTitle="Birthday" [nzSpan]="1">February 2,1900</nz-descriptions-item>
  31. <nz-descriptions-item nzTitle="Website" [nzSpan]="1"> - </nz-descriptions-item>
  32. <nz-descriptions-item nzTitle="Message" [nzSpan]="2">
  33. Make things as simple as possible but no simpler.
  34. </nz-descriptions-item>
  35. </nz-descriptions>
  36. <nz-divider></nz-divider>
  37. <nz-descriptions [nzColumn]="2" nzTitle="Company">
  38. <nz-descriptions-item nzTitle="Position" [nzSpan]="1">Programmer</nz-descriptions-item>
  39. <nz-descriptions-item nzTitle="Responsibilities" [nzSpan]="1">Coding</nz-descriptions-item>
  40. <nz-descriptions-item nzTitle="Department" [nzSpan]="1">AFX</nz-descriptions-item>
  41. <nz-descriptions-item nzTitle="Supervisor" [nzSpan]="1">Lin</nz-descriptions-item>
  42. <nz-descriptions-item nzTitle="Skills" [nzSpan]="2">
  43. C / C + +, data structures, software engineering, operating systems, computer networks, databases, compiler theory, computer
  44. architecture, Microcomputer Principle and Interface Technology, Computer English, Java, ASP, etc.
  45. </nz-descriptions-item>
  46. </nz-descriptions>
  47. <nz-divider></nz-divider>
  48. <nz-descriptions [nzColumn]="2" nzTitle="Contacts">
  49. <nz-descriptions-item nzTitle="Email" [nzSpan]="1">AntDesign@example.com</nz-descriptions-item>
  50. <nz-descriptions-item nzTitle="Phone Number" [nzSpan]="1">+86 181 0000 0000</nz-descriptions-item>
  51. <nz-descriptions-item nzTitle="Github" [nzSpan]="2">
  52. <a href="https://github.com/NG-ZORRO/ng-zorro-antd" target="_blank">github.com/NG-ZORRO/ng-zorro-antd</a>
  53. </nz-descriptions-item>
  54. </nz-descriptions>
  55. </nz-drawer>
  56. `,
  57. styles: [
  58. `
  59. .title {
  60. font-size: 16px;
  61. color: rgba(0, 0, 0, 0.85);
  62. line-height: 24px;
  63. display: block;
  64. margin-bottom: 16px;
  65. }
  66. .item-wrap {
  67. font-size: 14px;
  68. line-height: 22px;
  69. margin-bottom: 7px;
  70. color: rgba(0, 0, 0, 0.65);
  71. }
  72. .label {
  73. margin-right: 8px;
  74. display: inline-block;
  75. color: rgba(0, 0, 0, 0.85);
  76. }
  77. `
  78. ]
  79. })
  80. export class NzDemoDrawerUserProfileComponent {
  81. data = [
  82. {
  83. name: 'Lily'
  84. },
  85. {
  86. name: 'Lily'
  87. }
  88. ];
  89. visible = false;
  90. open(): void {
  91. this.visible = true;
  92. }
  93. close(): void {
  94. this.visible = false;
  95. }
  96. }

Drawer抽屉 - 图6

服务方式创建

Drawer 的 service 用法,示例中演示了用户自定义模板、自定义component。

  1. /* declarations: NzDrawerCustomComponent */
  2. import { Component, Input, TemplateRef, ViewChild } from '@angular/core';
  3. import { NzDrawerRef, NzDrawerService } from 'ng-zorro-antd/drawer';
  4. @Component({
  5. selector: 'nz-demo-drawer-service',
  6. template: `
  7. <ng-template #drawerTemplate let-data let-drawerRef="drawerRef">
  8. value: {{ data?.value }}
  9. <br />
  10. <button nz-button nzType="primary" (click)="drawerRef.close()">close</button>
  11. </ng-template>
  12. <div nz-form>
  13. <nz-form-item>
  14. <input nz-input [(ngModel)]="value" />
  15. </nz-form-item>
  16. </div>
  17. <button nz-button nzType="primary" (click)="openTemplate()">Use Template</button>&nbsp;
  18. <button nz-button nzType="primary" (click)="openComponent()">Use Component</button>
  19. `
  20. })
  21. export class NzDemoDrawerServiceComponent {
  22. @ViewChild('drawerTemplate', { static: false }) drawerTemplate?: TemplateRef<{
  23. $implicit: { value: string };
  24. drawerRef: NzDrawerRef<string>;
  25. }>;
  26. value = 'ng';
  27. constructor(private drawerService: NzDrawerService) {}
  28. openTemplate(): void {
  29. const drawerRef = this.drawerService.create({
  30. nzTitle: 'Template',
  31. nzContent: this.drawerTemplate,
  32. nzContentParams: {
  33. value: this.value
  34. }
  35. });
  36. drawerRef.afterOpen.subscribe(() => {
  37. console.log('Drawer(Template) open');
  38. });
  39. drawerRef.afterClose.subscribe(() => {
  40. console.log('Drawer(Template) close');
  41. });
  42. }
  43. openComponent(): void {
  44. const drawerRef = this.drawerService.create<NzDrawerCustomComponent, { value: string }, string>({
  45. nzTitle: 'Component',
  46. nzContent: NzDrawerCustomComponent,
  47. nzContentParams: {
  48. value: this.value
  49. }
  50. });
  51. drawerRef.afterOpen.subscribe(() => {
  52. console.log('Drawer(Component) open');
  53. });
  54. drawerRef.afterClose.subscribe(data => {
  55. console.log(data);
  56. if (typeof data === 'string') {
  57. this.value = data;
  58. }
  59. });
  60. }
  61. }
  62. @Component({
  63. selector: 'nz-drawer-custom-component',
  64. template: `
  65. <div>
  66. <input nz-input [(ngModel)]="value" />
  67. <nz-divider></nz-divider>
  68. <button nzType="primary" (click)="close()" nz-button>Confirm</button>
  69. </div>
  70. `
  71. })
  72. export class NzDrawerCustomComponent {
  73. @Input() value = '';
  74. constructor(private drawerRef: NzDrawerRef<string>) {}
  75. close(): void {
  76. this.drawerRef.close(this.value);
  77. }
  78. }

API

nz-drawercomponent

参数说明类型默认值全局配置
[nzClosable]是否显示右上角的关闭按钮booleantrue
[nzMaskClosable]点击蒙层是否允许关闭booleantrue
[nzMask]是否展示遮罩booleantrue
[nzCloseOnNavigation]导航历史变化时是否关闭抽屉组件booleantrue
[nzMaskStyle]遮罩样式object{}
[nzKeyboard]是否支持键盘 esc 关闭booleantrue
[nzBodyStyle]Drawer body 样式object{}
[nzTitle]标题string | TemplateRef<void>-
[nzVisible]Drawer 是否可见boolean-
[nzPlacement]抽屉的方向‘top’ | ‘right’ | ‘bottom’ | ‘left’‘right’
[nzWidth]宽度, 只在方向为 ‘right’‘left’ 时生效number | string256
[nzHeight]高度, 只在方向为 ‘top’‘bottom’ 时生效number | string256
[nzOffsetX]x 坐标移量(px), 只在方向为 ‘right’‘left’ 时生效number0
[nzOffsetY]y 坐标移量(px), 高度, 只在方向为 ‘top’‘bottom’ 时生效number0
[nzWrapClassName]对话框外层容器的类名string-
[nzZIndex]设置 Drawer 的 z-indexnumber1000
(nzOnClose)点击遮罩层或右上角叉的回调EventEmitter<MouseEvent>-

NzDrawerServiceservice

方法名说明参数返回
create<T, D, R>创建并打开一个 DrawerNzDrawerOptions<T, D>NzDrawerRef<T, R>

NzDrawerOptions

参数说明类型默认值
nzContentDrawer body 的内容TemplateRef<{ $implicit: D, drawerRef: NzDrawerRef }> | Type<T>-
nzContentParams内容组件的输入参数 / Template的 contextD-
nzOnCancel点击遮罩层或右上角叉时执行,该函数可返回 promise 待执行完毕或 promise 结束时,将自动关闭对话框(返回false可阻止关闭)() => Promise<any>-
nzClosable是否显示右上角的关闭按钮booleantrue
nzMaskClosable点击蒙层是否允许关闭booleantrue
nzMask是否展示遮罩booleantrue
nzCloseOnNavigation导航历史变化时是否关闭抽屉组件booleantrue
nzKeyboard是否支持键盘esc关闭booleantrue
nzMaskStyle遮罩样式object{}
nzBodyStyleModal body 样式object{}
nzTitle标题string | TemplateRef<void>-
nzWidth宽度number | string256
nzHeight高度, 只在方向为 ‘top’‘bottom’ 时生效number | string256
nzWrapClassName对话框外层容器的类名string-
nzZIndex设置 Drawer 的 z-indexnumber1000
nzPlacement抽屉的方向‘top’ | ‘right’ | ‘bottom’ | ‘left’‘right’
nzOffsetXx 坐标移量(px)number0
nzOffsetYy 坐标移量(px), 高度, 只在方向为 ‘top’‘bottom’ 时生效number0

NzDrawerRef

方法

名称说明类型
close关闭 Drawer(result?: R) => void
open打开 Drawer() => void
getContentComponent返回 nzContent 为组件时的组件实例() => T | null

属性

名称说明类型
afterOpen打开之后的回调Observable<void>
afterClose关闭之后的回调Observable<R>
nzClosable是否显示右上角的关闭按钮boolean
nzMaskClosable点击蒙层是否允许关闭boolean
nzMask是否展示遮罩boolean
nzMaskStyle遮罩样式object
nzKeyboard是否支持键盘esc关闭boolean
nzBodyStyleModal body 样式object
nzTitle标题string | TemplateRef<void>
nzWidth宽度number | string
nzHeight高度, 只在方向为 ‘top’‘bottom’ 时生效number | string
nzWrapClassName对话框外层容器的类名string
nzZIndex设置 Drawer 的 z-indexnumber
nzPlacement抽屉的方向‘top’ | ‘right’ | ‘bottom’ | ‘left’
nzOffsetXx 坐标移量(px)number
nzOffsetYy 坐标移量(px), 高度, 只在方向为 ‘top’‘bottom’ 时生效number