How to get the next item in ng-repeat for an object?

天涯浪子 提交于 2019-12-24 01:26:07

问题


I have an object as follows:

scope.items = {
"poor-John-1": {
    "_id": "poor-John-1",
    "name": "poor-John-1",
    "sName": "room-poor-John-2"
},
"poor-John-2": {
    "_id": "poor-John-2",
    "name": "poor-John-2",
    "sName": "room-poor-John-2"
}

}

I have rendered the object in the following way

<tr ng-repeat="item in items | orderObjectBy: '_id' track by item._id"  ng-class="getStyle(item, next_item)"><td>{{item.sName}}</td></tr>

What I want to do is, pass, the current and next_item to the getStyle function, since its not an array, $index is not pointing the value. I have data in object.


回答1:


Maybe you can do this with

$scope.keys = Object.keys($scope.items) // put keys on scope

// get next from keys
<tr ng-repeat="(key, item) in items track by item._id"  ng-class="getStyle(item, items[keys[$index +1]])">
  <td>{{key}}</td>
  <td>{{item.name}}</td>
  <td>next : {{items[keys[$index +1]]}}</td>
  </tr>

Look this, tell me if it's what you want https://plnkr.co/edit/GCNmWb9Qv2mntqTnGsX1?p=preview



来源:https://stackoverflow.com/questions/37548400/how-to-get-the-next-item-in-ng-repeat-for-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!