Angular 4-如何在ngFor循环内为标签提供唯一的ID值? [英] Angular 4 - How to have a unique id value for a tag inside an ngFor loop?
本文介绍了Angular 4-如何在ngFor循环内为标签提供唯一的ID值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到一个问题,我需要在角度4的*ngFor
循环中包含的每个数据行都有唯一的id值.
I came across a problem in which I need to have a unique id value for each data row I have inside my *ngFor
loop in angular 4.
我的代码如下:
<div *ngFor="let row of myDataList">
<div id="thisNeedsToBeUnique">{{ row.myValue }}</div>
</div>
推荐答案
假设您的对象内有一个唯一的id值,则可以这样做:
Assuming that you have a unique id value inside your object, then you can do it like this:
<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>
您还可以像这样将字符串和动态值一起隐藏:
You can also concatinate a string and your dynamic value together like this:
<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>
这篇关于Angular 4-如何在ngFor循环内为标签提供唯一的ID值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文