Notification通知提醒框

全局展示通知提醒信息。

何时使用

在系统四个角显示通知提醒信息。经常用于以下情况:

  • 较为复杂的通知内容。
  • 带有交互的通知,给出用户下一步的行动点。
  • 系统主动推送。

如何使用

NzMessage类似,如果要修改全局默认配置,你可以设置提供商 NZ_NOTIFICATION_CONFIG 的值来修改。(如:在你的模块的providers中加入 { provide: NZ_NOTIFICATION_CONFIG, useValue: { nzDuration: 3000 }}NZ_NOTIFICATION_CONFIG 可以从 ng-zorro-antd 中导入)

默认全局配置为:

  1. {
  2. nzTop : '24px',
  3. nzBottom : '24px',
  4. nzPlacement : 'topRight',
  5. nzDuration : 4500,
  6. nzMaxStack : 7,
  7. nzPauseOnHover: true,
  8. nzAnimate : true
  9. }

代码演示

Notification通知提醒框 - 图1

基本

最简单的用法,4.5 秒后自动关闭。

  1. import { Component } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-basic',
  5. template: `
  6. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
  7. `,
  8. styles: []
  9. })
  10. export class NzDemoNotificationBasicComponent {
  11. constructor(private notification: NzNotificationService) {}
  12. createBasicNotification(): void {
  13. this.notification.blank(
  14. 'Notification Title',
  15. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
  16. );
  17. }
  18. }

Notification通知提醒框 - 图2

带有图标的通知提醒框

通知提醒框左侧有图标。

  1. import { Component } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-with-icon',
  5. template: `
  6. <button nz-button (click)="createNotification('success')">Success</button>
  7. <button nz-button (click)="createNotification('info')">Info</button>
  8. <button nz-button (click)="createNotification('warning')">Warning</button>
  9. <button nz-button (click)="createNotification('error')">Error</button>
  10. `,
  11. styles: [
  12. `
  13. button {
  14. margin-right: 1em;
  15. }
  16. `
  17. ]
  18. })
  19. export class NzDemoNotificationWithIconComponent {
  20. createNotification(type: string): void {
  21. this.notification.create(
  22. type,
  23. 'Notification Title',
  24. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
  25. );
  26. }
  27. constructor(private notification: NzNotificationService) {}
  28. }

Notification通知提醒框 - 图3

自定义图标

图标可以被自定义。

  1. import { Component, TemplateRef } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-custom-icon',
  5. template: `
  6. <ng-template #template>
  7. <div class="ant-notification-notice-content">
  8. <div class="ant-notification-notice-with-icon">
  9. <span class="ant-notification-notice-icon"
  10. ><i nz-icon type="smile" style="color: rgb(16, 142, 233);"></i
  11. ></span>
  12. <div class="ant-notification-notice-message">Notification Title</div>
  13. <div class="ant-notification-notice-description">
  14. This is the content of the notification. This is the content of the notification. This is the content of the
  15. notification.
  16. </div>
  17. </div>
  18. </div>
  19. </ng-template>
  20. <button nz-button [nzType]="'primary'" (click)="createBasicNotification(template)">
  21. Open the notification box
  22. </button>
  23. `,
  24. styles: []
  25. })
  26. export class NzDemoNotificationCustomIconComponent {
  27. constructor(private notification: NzNotificationService) {}
  28. createBasicNotification(template: TemplateRef<{}>): void {
  29. this.notification.template(template);
  30. }
  31. }

Notification通知提醒框 - 图4

自定义样式

使用 nzStyle 和 nzClass 来定义样式。

  1. import { Component } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-custom-style',
  5. template: `
  6. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
  7. `,
  8. styles: []
  9. })
  10. export class NzDemoNotificationCustomStyleComponent {
  11. constructor(private notification: NzNotificationService) {}
  12. createBasicNotification(): void {
  13. this.notification.blank(
  14. 'Notification Title',
  15. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
  16. {
  17. nzStyle: {
  18. width: '600px',
  19. marginLeft: '-265px'
  20. },
  21. nzClass: 'test-class'
  22. }
  23. );
  24. }
  25. }

Notification通知提醒框 - 图5

使用模板

通过模板来实现更加复杂的效果和交互。

  1. import { Component, TemplateRef, ViewChild } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-template',
  5. template: `
  6. <button nz-button [nzType]="'primary'" (click)="ninja()">Open the notification box</button>
  7. <ng-template let-fruit="data">
  8. It's a <nz-tag [nzColor]="fruit.color">{{ fruit.name }}</nz-tag>
  9. <button nz-button nzType="small">Cut It!</button>
  10. </ng-template>
  11. `,
  12. styles: [
  13. `
  14. button {
  15. margin-top: 8px;
  16. }
  17. `
  18. ]
  19. })
  20. export class NzDemoNotificationTemplateComponent {
  21. @ViewChild(TemplateRef) template: TemplateRef<{}>;
  22. ninja(): void {
  23. const fruits = [
  24. { name: 'Apple', color: 'red' },
  25. { name: 'Orange', color: 'orange' },
  26. { name: 'Watermelon', color: 'green' }
  27. ];
  28. fruits.forEach(fruit => {
  29. this.notificationService.template(this.template, { nzData: fruit });
  30. });
  31. }
  32. constructor(private notificationService: NzNotificationService) {}
  33. }

Notification通知提醒框 - 图6

自动关闭的延时

自定义通知框自动关闭的延时,默认4.5s,取消自动关闭只要将该值设为 0 即可。

  1. import { Component } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-duration',
  5. template: `
  6. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
  7. `,
  8. styles: []
  9. })
  10. export class NzDemoNotificationDurationComponent {
  11. createBasicNotification(): void {
  12. this.notification.blank(
  13. 'Notification Title',
  14. 'I will never close automatically. I will be close automatically. I will never close automatically.',
  15. { nzDuration: 0 }
  16. );
  17. }
  18. constructor(private notification: NzNotificationService) {}
  19. }

Notification通知提醒框 - 图7

自定义按钮

自定义关闭按钮的样式和文字。

  1. import { Component, TemplateRef } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-with-btn',
  5. template: `
  6. <ng-template #template let-notification>
  7. <div class="ant-notification-notice-content">
  8. <div>
  9. <div class="ant-notification-notice-message">Notification Title</div>
  10. <div class="ant-notification-notice-description">
  11. A function will be be called after the notification is closed (automatically after the "duration" time of
  12. manually).
  13. </div>
  14. <span class="ant-notification-notice-btn">
  15. <button nz-button nzType="primary" nzSize="small" (click)="notification.close()">
  16. <span>Confirm</span>
  17. </button>
  18. </span>
  19. </div>
  20. </div>
  21. </ng-template>
  22. <button nz-button [nzType]="'primary'" (click)="createBasicNotification(template)">
  23. Open the notification box
  24. </button>
  25. `,
  26. styles: []
  27. })
  28. export class NzDemoNotificationWithBtnComponent {
  29. constructor(private notification: NzNotificationService) {}
  30. createBasicNotification(template: TemplateRef<{}>): void {
  31. this.notification.template(template);
  32. }
  33. }

Notification通知提醒框 - 图8

位置

可以设置通知从右上角、右下角、左下角、左上角弹出。

  1. import { Component } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-placement',
  5. template: `
  6. <nz-select
  7. [(ngModel)]="placement"
  8. style="width: 120px; margin-right: 10px;"
  9. (ngModelChange)="clearBeforeNotifications()"
  10. >
  11. <nz-option nzValue="topLeft" nzLabel="topLeft"></nz-option>
  12. <nz-option nzValue="topRight" nzLabel="topRight"></nz-option>
  13. <nz-option nzValue="bottomLeft" nzLabel="bottomLeft"></nz-option>
  14. <nz-option nzValue="bottomRight" nzLabel="bottomRight"></nz-option>
  15. </nz-select>
  16. <button nz-button [nzType]="'primary'" (click)="createBasicNotification()">Open the notification box</button>
  17. `,
  18. styles: []
  19. })
  20. export class NzDemoNotificationPlacementComponent {
  21. placement = 'topRight';
  22. clearBeforeNotifications(): void {
  23. this.notification.remove();
  24. }
  25. createBasicNotification(): void {
  26. this.notification.config({
  27. nzPlacement: this.placement
  28. });
  29. this.notification.blank(
  30. 'Notification Title',
  31. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
  32. );
  33. }
  34. constructor(private notification: NzNotificationService) {}
  35. }

Notification通知提醒框 - 图9

更新消息内容

可以通过唯一的 nzKey 来更新内容。

  1. import { Component } from '@angular/core';
  2. import { NzNotificationService } from 'ng-zorro-antd';
  3. @Component({
  4. selector: 'nz-demo-notification-update',
  5. template: `
  6. <button nz-button [nzType]="'primary'" (click)="createAutoUpdatingNotifications()">
  7. Open the notification box
  8. </button>
  9. `,
  10. styles: []
  11. })
  12. export class NzDemoNotificationUpdateComponent {
  13. constructor(private notification: NzNotificationService) {}
  14. createAutoUpdatingNotifications(): void {
  15. this.notification.blank('Notification Title', 'Description.', {
  16. nzKey: 'key'
  17. });
  18. setTimeout(() => {
  19. this.notification.blank('New Title', 'New description', {
  20. nzKey: 'key'
  21. });
  22. }, 1000);
  23. }
  24. }

API

单独引入此组件

想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。

  1. import { NzNotificationModule } from 'ng-zorro-antd';

NzNotificationServiceservice

组件提供了一些服务方法,使用方式和参数如下:

  • NzNotificationService.blank(title, content, [options]) // 不带图标的提示
  • NzNotificationService.success(title, content, [options])
  • NzNotificationService.error(title, content, [options])
  • NzNotificationService.info(title, content, [options])
  • NzNotificationService.warning(title, content, [options])
    参数说明类型默认值
    title标题string-
    content提示内容string-
    options支持设置针对当前提示框的参数,见下方表格object-

options 支持设置的参数如下:

参数说明类型
nzKey通知提示的唯一标识符string
nzDuration持续时间(毫秒),当设置为 0 时不消失number
nzPauseOnHover鼠标移上时禁止自动移除boolean
nzAnimate开关动画效果boolean
nzStyle自定义内联样式object
nzClass自定义 CSS classobject
nzData任何想要在模板中作为上下文的数据any

还提供了全局销毁方法:

  • NzNotificationService.remove(id) // 移除特定id的消息,当id为空时,移除所有消息(该消息id通过上述方法返回值中得到)

全局配置(NZ_MESSAGE_CONFIG)

参数说明类型默认值
nzDuration持续时间(毫秒),当设置为0时不消失number4500
nzMaxStack同一时间可展示的最大提示数量number8
nzPauseOnHover鼠标移上时禁止自动移除booleantrue
nzAnimate开关动画效果booleantrue
nzTop消息从顶部弹出时,距离顶部的位置。string24px
nzBottom消息从底部弹出时,距离底部的位置。string24px
nzPlacement弹出位置,可选 topLefttopRightbottomLeftbottomRightstringtopRight

NzNotificationDataFilled

当你调用 NzNotificationService.success 或其他方法时会返回该对象。

  1. export interface NzNotificationDataFilled {
  2. onClose: Subject<boolean>; // 当 notification 关闭时它会派发一个事件,如果为用户手动关闭会派发 `true`
  3. }