2014-04-25 25 views
0

有沒有辦法讓ng-repeat當前記錄索引?我試圖給每個記錄div一個ID等於記錄索引的例子:Angularjs ng-repeat index

<div id="1"> 
<div id="2"> 
<div id="3"> 

任何示例都是高度apreciated。

我使用的是NG重複如下:

 <div ng-repeat="rows in clients.Result"> 
+1

的可能重複[動態ID NG-重複](http://stackoverflow.com/questions/15301746/dynamic-id-ng -repeat) –

回答

4

使用$指標與評價捲髮:

<div id="{{$index}}" ng-repeat="rows in clients.Result"> 

PLNKR

+1

@mchan確保你理解索引從0開始,所以如果你想像你的演示一樣使用'1','2','3',那麼你可以使用'id =「{{$ index + 1 }}「'。另請注意,在轉發器上運行過濾器將更改索引以反映可見列表。 – jnthnjns

+0

你是對的..這就是爲什麼我添加了一個plnkr .. –

+0

你的例子看起來不錯,只是爲OP添加了一些旁註。我已經給你+1了:) – jnthnjns

4

$index包含當前索引。所以你可以在你的ng-repeat裏面使用它,比如{{$index}}

所有這些變量都記錄在ng-repeat documentation的最頂端。