![](/img/trans.png)
[英]how to send an ID from an array that is inside a *ngFor to another component in Angular
[英]Angular 4 - How to have a unique id value for a tag inside an ngFor loop?
我遇到了一个问题,我需要在角度4的*ngFor
循环中拥有的每个数据行都有唯一的id值。
我的代码是这样的:
<div *ngFor="let row of myDataList">
<div id="thisNeedsToBeUnique">{{ row.myValue }}</div>
</div>
假设您的对象内有一个唯一的id值,则可以这样进行操作:
<div *ngFor="let row of myDataList">
<div [attr.id]="row.myId">{{ row.myValue }}</div>
</div>
您也可以这样:
<div *ngFor="let row of myDataList">
<div attr.id="{{row.myId}}">{{ row.myValue }}</div>
</div>
您还可以像这样将字符串和动态值一起隐藏起来:
<div *ngFor="let row of myDataList">
<div [attr.id]="'myString' + row.myId">{{ row.myValue }}</div>
</div>
<!-- Or -->
<div *ngFor="let row of myDataList">
<div attr.id="myString{{row.myId}}">{{ row.myValue }}</div>
</div>
使用索引生成唯一的ID
<div *ngFor="let row of myDataList; let i = index">
<div [attr.id]="row.myValue + i">{{ row.myValue }}</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.