angularJS转发器中的错误键

时间:2013-12-12 18:35:00

标签: javascript angularjs repeater angularjs-ng-repeat

我有数据:

[Object, Object, Object]
  0: Object
    $$hashKey: "007"
    amount: "123111"
    name: "test"
    number: "5"
    position: "ttt"
  1: Object
    $$hashKey: "006"
    amount: "123111"
    name: "test4"
    number: "4"
    position: "поз"
  2: Object
    $$hashKey: "005"
    amount: "34555"
    name: "еее"
    number: "1"
    position: "вапвап"

我在html中有转发器:

<tr ng-repeat="(key, player) in players | orderBy:'number'">
    <td>{{key}} - {{player.number}}</td>
    <td>{{player.name}}</td>
    <td>{{player.position}}</td>
    <td>{{player.amount}}</td>
    <td>
        <button ng-click="edit(key)" class="btn btn-primary"><span class="glyphicon glyphicon-edit"></span></button>
        <button ng-click="delete(key)" class="btn btn-danger"><span class="glyphicon glyphicon-floppy-remove"></span></button>
    </td>
</tr>

我需要从转发器中当前元素的数据中获取密钥,但是我获得了订单值的密钥。

0 - 1   еее вапвап  34555     
1 - 4   test4   поз 123111    
2 - 5   test    ttt 123111

但我需要:

2 - 1   еее вапвап  34555     
1 - 4   test4   поз 123111    
0 - 5   test    ttt 123111

2 个答案:

答案 0 :(得分:0)

你做不到。列表值的顺序发生变化,这意味着在使用ng-repeat观察之前,您将获得每个索引的新值。您应该将索引设置为播放器的属性。然后使用该属性。

for(var i in players) {
    players[i].index = i;
}

答案 1 :(得分:0)

$scope.getRealId = function(hashKey) {
    for (var i in $scope.players) {
        if ($scope.players[i].$$hashKey === hashKey) {
            return i;
        }
    }
};

和ng-repeat内部:

{{getRealId(player.$$hashKey)}}

<button ng-click="edit(getRealId(player.$$hashKey))" class="btn btn-primary"><span class="glyphicon glyphicon-edit"></span></button>