如何从ngFor向html属性值添加动态索引 [英] How to add dynamic index from ngFor to html attribute value

查看:131
本文介绍了如何从ngFor向html属性值添加动态索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行ngFor,我需要循环内的某些属性通过向其添加ngFor索引来更改其值.这意味着在ngFor中创建的每个div都将具有uniq属性值. 来源:

I run ngFor and I need that some attribute inside the loop would change it's value by adding to it ngFor index. That's mean that each div that is created in ngFor will have uniq attribute value. Source :

<div class="class1" *ngFor="let item of items; let i= index">
    <div class="card-header" role="tab" id="Id">
        <h1>Hello</h1>
    </div>

我想绑定 id 以获取其值:当index = 0时为id0.

I want to bind id to get it's value: Id0 when index=0.

<div class="card-header" role="tab" [attr.id]="Id+'i'"> Doesn't work :(

推荐答案

尝试:

<div class="card-header" role="tab" id="{{'Id'+i}}">

这篇关于如何从ngFor向html属性值添加动态索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆