Angular8 / NG-ZORRO 8:在 nzAction 的 ng-template 中绑定

Angular8 / NG-ZORRO 8: Binding in ng-template of nzAction

我正在使用 angular 8 和 ng-zorro 版本 8.2.1。

我有一张 nz-card 看到 https://ng.ant.design/components/card/en*ngFor 重复了。对于这些操作,我需要访问 *ngFor 的 属性 但事实证明我无法从模板中访问它。

<ng-template #boxActionDetails>
    <p>{{box.id}}</p>
</ng-template>
<nz-card *ngFor="let box of boxes" nzTitle="{{box.title}}" [nzActions]="[boxActionDetails]">
     <p>{{box.description}}</p>
</nz-card>

执行上面的代码时,出现如下js错误

TypeError: Cannot read property 'id' of undefined

不幸的是,我找不到解决问题的方法,所以我想出了以下解决方法:

[attr.boxid]="box._id"

将数据存储在 html 中
<nz-card *ngFor="let box of boxes" nzTitle="{{box.title}}"
            [nzActions]="[boxActionLearn, boxActionEdit, boxActionDetails, boxActionDelete]" class="box" [attr.boxid]="box._id">
            <p>{{box.description}}</p>

单击带有 (click)="actionDetails($event)"

的操作时,调用带有事件处理程序的通用函数
<ng-template #boxActionDetails>
        <i nz-icon nzType="unordered-list" (click)="actionDetails($event)"></i>
</ng-template>

在动作中,通过偶数目标迭代到包含数据的父级,并用于进一步处理。

actionDetails(event) {
    var target = event.target || event.srcElement || event.currentTarget;
    var boxid = target.parentNode.parentNode.parentNode.parentNode.parentNode.attributes.boxid.nodeValue;
}

你可以在循环体中声明ng-template,像这样:

<nz-card *ngFor="let item of [true, true, true]; index as i"
         nzTitle="Card title" 
         [nzActions]="[action]">
  <p>Card content</p>
  <p>Card content</p>
  <p>Card content</p>
  <ng-template #action>
   <button nz-button>{{i}}</button>
  </ng-template>
</nz-card>

Live DEMO