我使用的是角形8和ng-zorro版本8.2.1。
我有一个nz卡,参见由*ngFor重复的https://ng.ant.design/components/card/en。对于这些操作,我需要访问*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:无法读取未定义的属性'id‘
发布于 2019-09-01 10:24:03
不幸的是,我找不到任何解决办法来解决这个问题,所以我想出了以下解决办法:
使用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;
}发布于 2019-09-04 11:02:27
您可以在循环体中声明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>https://stackoverflow.com/questions/57737529
复制相似问题