Comment评论

对网站内容的反馈、评价和讨论。

何时使用

评论组件可用于对事物的讨论,例如页面、博客文章、问题等等。

  1. import { NzCommentModule } from 'ng-zorro-antd/comment';

代码演示Comment评论 - 图2

Comment评论 - 图3

基本评论

一个基本的评论组件,带有作者、头像、时间和操作。

  1. import { Component } from '@angular/core';
  2. import { formatDistance } from 'date-fns';
  3. @Component({
  4. selector: 'nz-demo-comment-basic',
  5. template: `
  6. <nz-comment nzAuthor="Han Solo" [nzDatetime]="time">
  7. <nz-avatar nz-comment-avatar nzIcon="user" nzSrc="//zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png"></nz-avatar>
  8. <nz-comment-content>
  9. <p>
  10. We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people
  11. create their product prototypes beautifully and efficiently.
  12. </p>
  13. </nz-comment-content>
  14. <nz-comment-action>
  15. <i nz-tooltip nzTitle="Like" nz-icon nzType="like" [nzTheme]="likes > 0 ? 'twotone' : 'outline'" (click)="like()"></i>
  16. <span class="count like">{{ likes }}</span>
  17. </nz-comment-action>
  18. <nz-comment-action>
  19. <i nz-tooltip nzTitle="Dislike" nz-icon nzType="dislike" [nzTheme]="dislikes > 0 ? 'twotone' : 'outline'" (click)="dislike()"></i>
  20. <span class="count dislike">{{ dislikes }}</span>
  21. </nz-comment-action>
  22. <nz-comment-action>Reply to</nz-comment-action>
  23. </nz-comment>
  24. `,
  25. styles: [
  26. `
  27. .count {
  28. padding-left: 8px;
  29. cursor: auto;
  30. }
  31. `
  32. ]
  33. })
  34. export class NzDemoCommentBasicComponent {
  35. likes = 0;
  36. dislikes = 0;
  37. time = formatDistance(new Date(), new Date());
  38. like(): void {
  39. this.likes = 1;
  40. this.dislikes = 0;
  41. }
  42. dislike(): void {
  43. this.likes = 0;
  44. this.dislikes = 1;
  45. }
  46. }

Comment评论 - 图4

配合列表组件

配合 nz-list 组件展现评论列表。

  1. import { Component } from '@angular/core';
  2. import { addDays, formatDistance } from 'date-fns';
  3. @Component({
  4. selector: 'nz-demo-comment-list',
  5. template: `
  6. <nz-list [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'">
  7. <ng-template #item let-item>
  8. <nz-comment [nzAuthor]="item.author" [nzDatetime]="item.datetime">
  9. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="item.avatar"></nz-avatar>
  10. <nz-comment-content>
  11. <p>{{ item.content }}</p>
  12. </nz-comment-content>
  13. <nz-comment-action>Reply to</nz-comment-action>
  14. </nz-comment>
  15. </ng-template>
  16. </nz-list>
  17. `
  18. })
  19. export class NzDemoCommentListComponent {
  20. data = [
  21. {
  22. author: 'Han Solo',
  23. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  24. content:
  25. 'We supply a series of design principles, practical patterns and high quality design resources' +
  26. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  27. datetime: formatDistance(new Date(), addDays(new Date(), 1))
  28. },
  29. {
  30. author: 'Han Solo',
  31. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  32. content:
  33. 'We supply a series of design principles, practical patterns and high quality design resources' +
  34. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  35. datetime: formatDistance(new Date(), addDays(new Date(), 2))
  36. }
  37. ];
  38. }

Comment评论 - 图5

嵌套评论

评论可以嵌套。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'nz-demo-comment-nested',
  4. template: `
  5. <ng-template #commentTemplateRef let-comment="comment">
  6. <nz-comment [nzAuthor]="comment.author">
  7. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="comment.avatar"></nz-avatar>
  8. <nz-comment-content>
  9. <p>{{ comment.content }}</p>
  10. </nz-comment-content>
  11. <nz-comment-action>Reply to</nz-comment-action>
  12. <ng-container *ngIf="comment.children && comment.children.length">
  13. <ng-template ngFor let-child [ngForOf]="comment.children">
  14. <ng-template [ngTemplateOutlet]="commentTemplateRef" [ngTemplateOutletContext]="{ comment: child }"> </ng-template>
  15. </ng-template>
  16. </ng-container>
  17. </nz-comment>
  18. </ng-template>
  19. <ng-template [ngTemplateOutlet]="commentTemplateRef" [ngTemplateOutletContext]="{ comment: data }"> </ng-template>
  20. `
  21. })
  22. export class NzDemoCommentNestedComponent {
  23. data = {
  24. author: 'Han Solo',
  25. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  26. content:
  27. 'We supply a series of design principles, practical patterns and high quality design resources' +
  28. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  29. children: [
  30. {
  31. author: 'Han Solo',
  32. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  33. content:
  34. 'We supply a series of design principles, practical patterns and high quality design resources' +
  35. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  36. children: [
  37. {
  38. author: 'Han Solo',
  39. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  40. content:
  41. 'We supply a series of design principles, practical patterns and high quality design resources' +
  42. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
  43. },
  44. {
  45. author: 'Han Solo',
  46. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  47. content:
  48. 'We supply a series of design principles, practical patterns and high quality design resources' +
  49. '(Sketch and Axure), to help people create their product prototypes beautifully and efficiently.'
  50. }
  51. ]
  52. }
  53. ]
  54. };
  55. }

Comment评论 - 图6

回复框

评论编辑器组件提供了相同样式的封装以支持自定义评论编辑器。

  1. import { Component } from '@angular/core';
  2. import { formatDistance } from 'date-fns';
  3. @Component({
  4. selector: 'nz-demo-comment-editor',
  5. template: `
  6. <nz-list *ngIf="data.length" [nzDataSource]="data" [nzRenderItem]="item" [nzItemLayout]="'horizontal'">
  7. <ng-template #item let-item>
  8. <nz-comment [nzAuthor]="item.author" [nzDatetime]="item.displayTime">
  9. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="item.avatar"></nz-avatar>
  10. <nz-comment-content>
  11. <p>{{ item.content }}</p>
  12. </nz-comment-content>
  13. </nz-comment>
  14. </ng-template>
  15. </nz-list>
  16. <nz-comment>
  17. <nz-avatar nz-comment-avatar nzIcon="user" [nzSrc]="user.avatar"></nz-avatar>
  18. <nz-comment-content>
  19. <nz-form-item>
  20. <textarea [(ngModel)]="inputValue" nz-input rows="4"></textarea>
  21. </nz-form-item>
  22. <nz-form-item>
  23. <button nz-button nzType="primary" [nzLoading]="submitting" [disabled]="!inputValue" (click)="handleSubmit()">
  24. Add Comment
  25. </button>
  26. </nz-form-item>
  27. </nz-comment-content>
  28. </nz-comment>
  29. `
  30. })
  31. export class NzDemoCommentEditorComponent {
  32. // tslint:disable-next-line:no-any
  33. data: any[] = [];
  34. submitting = false;
  35. user = {
  36. author: 'Han Solo',
  37. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png'
  38. };
  39. inputValue = '';
  40. handleSubmit(): void {
  41. this.submitting = true;
  42. const content = this.inputValue;
  43. this.inputValue = '';
  44. setTimeout(() => {
  45. this.submitting = false;
  46. this.data = [
  47. ...this.data,
  48. {
  49. ...this.user,
  50. content,
  51. datetime: new Date(),
  52. displayTime: formatDistance(new Date(), new Date())
  53. }
  54. ].map(e => {
  55. return {
  56. ...e,
  57. displayTime: formatDistance(new Date(), e.datetime)
  58. };
  59. });
  60. }, 800);
  61. }
  62. }

API

nz-commentcomponent

PropertyDescriptionTypeDefault
[nzAuthor]显示评论的作者string | TemplateRef<void>-
[nzDatetime]展示时间描述string | TemplateRef<void>-

[nz-comment-avatar]directive

要显示为评论头像的元素。

nz-comment-contentcomponent

评论的主要内容。

nz-comment-actioncomponent

在评论内容下面呈现的操作项。