angularJS中继器按键错误

angularJS wrong key in repeater

本文关键字:错误 中继器 angularJS      更新时间:2023-09-26

我有数据:

[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

你不能。列表值的顺序会发生变化,这意味着在使用ng repeat查看之前,您会为每个索引获取新值。您应该将索引设置为玩家的属性。然后改为使用该属性。

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

和内ng重复:

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

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