orderBy在ngRepeat中使用另一个变量属性

时间:2017-07-23 18:37:55

标签: angularjs angularjs-ng-repeat angularjs-orderby

是否可以通过仅使用orderBy而不是将count属性添加到数组中的项目来基于另一个变量对一个数组中的项目进行排序?

假设我在控制器中有一个数组和一个地图:

$scope.x = [{no:1,name:"a"},
    {no:2,name:"b"},
    {no:3,name:"c"}];

$scope.y = { 1: [1],
    2: [1,2,3],
    3: [1,2] };

并且html将如下所示:

<div ng-repeat="i in x | orderBy: y[i.no].length">
  {{i.no}}
  {{ y[i.no] }}
  {{ y[i.no].length }}
</div>

输出:

1 [1] 1
2 [1,2,3] 3
3 [1,2] 2

但它应该是:

1 [1] 1
3 [1,2] 2
2 [1,2,3] 3

1 个答案:

答案 0 :(得分:0)

您可以使用谓词函数来指定您的条件。

试试这个:

<div ng-repeat="i in x | orderBy: findOrder">
    {{i.no}}
    {{ y[i.no] }}
    {{ y[i.no].length }}
</div>

$scope.findOrder = function(elem){
      console.log(elem.no);
      return $scope.y[elem.no].length;
 }

A working fiddle