英文:
How to make the index from ngFor part of an html tag value
问题
我想将ngFor中的索引放到HTML中的data-test标签中。在HTML中是否可以这样做?
英文:
I have an ngFor loop set up like this:
<div *ngFor="let record of this.RecordsProcessed; let i = index">
   <div class="row my-16" data-test='test'_{{i}}>
        <div class="col-4">Id:</div>
        <div class="col-8">{{record?.Id}}</div>
    </div>
</div>
I want to put the index from ngFor on the data-text tag within the html. Is it possible to do something like this within the html?
答案1
得分: 1
尝试这样做:
<div *ngFor="let record of this.RecordsProcessed; let i = index">
    <div class="row my-16" [attr.data-test]="'test_' + i">
        <div class="col-4">Id:</div>
        <div class="col-8">{{record?.Id}}</div>
    </div>
</div>
   
[] 方括号告诉 Angular,一切都在双引号内部是 TypeScript 代码。
英文:
Try like this:
  <div *ngFor="let record of this.RecordsProcessed; let i = index">
  <div class="row my-16" [attr.data-test]="'test_' + i">
       <div class="col-4">Id:</div>
       <div class="col-8">{{record?.Id}}</div>
   </div>
</div>
[] brackets let angular know that everything inside of "" is typescript code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论