【问题标题】:How to get the next item in ng-repeat for an object?如何获取对象的 ng-repeat 中的下一个项目?
【发布时间】:2016-05-31 22:52:05
【问题描述】:

我有一个对象如下:

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"
}

}

我已经通过以下方式渲染了对象

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

我想要做的是,将 current 和 next_item 传递给 getStyle 函数,因为它不是数组,$index 没有指向值。我在对象中有数据。

【问题讨论】:

    标签: angularjs object angularjs-ng-repeat


    【解决方案1】:

    也许你可以这样做

    $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>
    

    看看这个,告诉我是不是你想要的https://plnkr.co/edit/GCNmWb9Qv2mntqTnGsX1?p=preview

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多