使用Angular JS生成动态HTML ID

时间:2014-10-15 18:21:34

标签: angularjs angularjs-ng-repeat

我目前有一个相当简单的表,如下所示:

<table id="responseGrid">
    <tbody>
        <tr id="response" data-ng-repeat="response in data.responses">
            <td id="Resp Key">{{response.key}}</td>
            <td id="Resp Value">{{response.value}}</td>
        </tr>
    </tbody>
</table>

但是,如果创建了多个行,则每个表列将具有相同的ID。我希望每个表列都有一个基于行的唯一ID。例如,我希望第一行的列带有ids&#34; Resp Key 1&#34;和&#34; Resp Value 1&#34;,然后下一行将包含带有ids&#34; Resp Key 2&#34;的列。和&#34; Resp Value 2&#34;等等。

在Angular JS中有没有办法做到这一点?我试着寻找某种获取我所处的响应索引的方法,但这似乎不起作用。此外,我无法找到一种连接ID的方法(虽然这可能更像是一个HTML问题)。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

你可以做这样的事情$index是数组的索引

    <tr id="response" data-ng-repeat="response in data.responses">
        <td id="{{'Resp Key '+($index+1)}}">{{response.key}}</td> // this will result in `Resp Key 1`,`Resp Key 2` ..
        <td id="{{'Resp Value '($index+1)}}">{{response.value}}</td>
    </tr>