Angular OrderBy子数组中第一项的属性

时间:2014-08-01 13:45:55

标签: angularjs

我有以下JSON

{
    "item": { 
        "id": 1
        "owners": [
            {
                "name": "bob"
            }
            {
                "name": "jane"
            }
        ]
    }
    "item": { 
        "id": 2
        "owners": [
            {
                "name": "jim"
            }
            {
                "name": "mary"
            }
        ]
    }
}

我试图按照第一个所有者排序,就像......

$scope.predicate = 'item.owners[0].name';
<div class="row item-row" ng-repeat="item in items | orderBy:predicate:reverse"></div>

这似乎不起作用,有没有正确的方法来做到这一点?

1 个答案:

答案 0 :(得分:2)

因为&#34;谓词&#34;在每个tail的上下文中进行评估,它应如下所示:owners[0].name

E.g:

<div ng-repeat="tail in tails | orderBy:predicate:reverse">

$scope.predicate = 'owners[0].name';
$scope.reverse   = false;   // or true

另请参阅此 short demo