orderBy降序在我的情况下不起作用?

时间:2014-04-15 05:38:02

标签: javascript angularjs

我希望看到基于points工作的decesecing订单,但它没有,这是演示http://jsfiddle.net/uRPSL/34/

纳克重复

<ul ng-repeat="friend in user">
    <li ng-repeat="relation in friend.relationship | orderBy:'points':true">    
      {{relation.name}}
    </li>
</ul>

JS

app.controller('Controller', function ($scope, $rootScope, $location) {
    $scope.user = [{
        'uId': 1,
            'name': 'Joe',
            'relationship': [{
            'uId': 2,
                'name': 'Jeremy',
                'tabs': [{
                'tabId': 1
            }],
                'tasks': [{
                'name': 'Im Jeremy Lin'
            }],
            'points': 50

        }]
    }, {
        'uId': 2,
            'name': 'justin',
            'relationship': [{
            'uId': 3,
                'name': 'Jordan',
                'tabs': [{
                'tabId': 2
            }],
                'tasks': [{
                'name': 'Im Jasmin Jacob'
            }],
            'points': 100

        }]
    }];
})

2 个答案:

答案 0 :(得分:0)

在您的示例中,每个关系数组中只有一个关系。如果向这些数组添加项目,则可以看到它们正在正确排序。以下是您的小提琴的更新,其中包含可见性的额外数据: http://jsfiddle.net/8Ub6n/

$scope.user = [
   {
      'uId':1,
      'name':'Joe',
      'relationship':[
         {
            'uId':2,
            'name':'Jeremy',
            'points':50
         },
         {
            'uId':2,
            'name':'Michael',
            'points':80
         }
      ]
   },
   {
      'uId':2,
      'name':'justin',
      'relationship':[
         {
            'uId':3,
            'name':'Jordan',
            'points':100
         },
         {
            'uId':4,
            'name':'Cameron',
            'points':15
         }
      ]
   }
];

答案 1 :(得分:0)

您可以通过以下方式在嵌套循环中执行此操作:

<ul ng-repeat="friend in user | orderBy:'relationship':'points':true">
    <li ng-repeat="relation in friend.relationship">    
      {{relation.name}}
    </li>
</ul>

此处orderBy:'Array':'expression':reverse