【问题标题】:ng-repeat on array of arrays, orderBy subarray index对数组数组进行 ng-repeat,orderBy 子数组索引
【发布时间】:2015-06-25 20:03:19
【问题描述】:

假设我有这个对象:

var rows = [
    [1, 2, 3, 4],
    [11, 222, 3333, 4444]
];

鉴于此,还有这个模板:

<tr ng-repeat="row in rows | orderBy ????">
    <td ng-repeat="cell in row">{{ cell }}</td>
</tr>

...如何按每行的第二个“列”(给定row 元素的索引1 处的值)订购ng-repeat

我是否纠正了 Angular 不支持这种情况——无需编写自定义排序函数? (我只是在做原型,所以使用ng-init 来定义我的范围变量而不是创建控制器。)

【问题讨论】:

    标签: angularjs angularjs-orderby


    【解决方案1】:

    确实如此。您可以按功能创建自定义订单。

    http://plnkr.co/edit/f6hCbHcLkrjyTODvDWjM?p=preview

    <div ng-repeat="row in rows | orderBy:secondIndex">
        {{row}}
    </div>
    
    //In controller
    $scope.secondIndex = function(arr){
        return arr[1];
    }
    

    【讨论】:

      【解决方案2】:

      你应该使用orderBy:'1':

      <tr ng-repeat="row in rows | orderBy:'1'">
         <td ng-repeat="cell in row">{{ cell }}</td>
      </tr>
      

      Demo

      【讨论】:

      • 赢家,鸡肉晚餐!
      • 太棒了。但我不明白为什么 orderBy 值是字符串?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多