ng-repeat on array of arrays, orderBy subarray index

半腔热情 提交于 2019-12-07 02:12:09

问题


Let's say I have this object:

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

Given that, and this template:

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

...how can I order an ng-repeat by the second "column" of each row (the value at index 1 of the given row element)?

Am I correct that Angular does not support this case—without having to write a custom sort function? (I'm just prototyping, so using ng-init to define my scope variables instead of creating a controller.)


回答1:


Actually it does. You can create custom order by functions.

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:


You just should to use orderBy:'1':

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

Demo



来源:https://stackoverflow.com/questions/31059735/ng-repeat-on-array-of-arrays-orderby-subarray-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!