如何使ngFor的索引成为HTML标记值的一部分。

huangapple go评论52阅读模式
英文:

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:

  &lt;div *ngFor=&quot;let record of this.RecordsProcessed; let i = index&quot;&gt;
  &lt;div class=&quot;row my-16&quot; [attr.data-test]=&quot;&#39;test_&#39; + i&quot;&gt;
       &lt;div class=&quot;col-4&quot;&gt;Id:&lt;/div&gt;
       &lt;div class=&quot;col-8&quot;&gt;{{record?.Id}}&lt;/div&gt;
   &lt;/div&gt;
&lt;/div&gt;

[] brackets let angular know that everything inside of "" is typescript code.

huangapple
  • 本文由 发表于 2023年6月2日 03:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定