AngularJS | OrderBy 不是数组

AngularJS | OrderBy is not an Array

我想为 table 列添加排序功能,但出现此错误:

[orderBy:notarray] Expected array but received: 0

此外,排序根本不起作用。虽然向上和向下箭头改变了位置,但调试器似乎获得了正确的信息。

我的HTML:

<th class="text-center"><a style="color:black"
    ng-click="sortColumn('severity')">
    Level
    <span ng-show="sortTable('severity')" class="fa fa-caret-down"></span>
    <span ng-show="sortTable('severity', false)" class="fa fa-caret-up"> 
    </span>
    </a>
</th>
<th><a style="color:black" ng-click="sortColumn('getTimeAgo')">
    Timestamp
    <span ng-show="sortTable('getTimeAgo')" class="fa fa-caret-down"> 
    </span>
    <span ng-show="sortTable('getTimeAgo', false)" class="fa fa-caret-up"> 
    </span>
    </a>
</th>

<tbody ng-repeat="t in Ctrl.notifications | filter : search | 
         filter : Ctrl.customFilter track by $index | 
         orderBy:sortingFn:sortReverse"
         ng-style="$index != 0 ? 'border-top:0px' : ''">

CTRL:

  $scope.sortColumn = (sortBy) => {
    $scope.sortReverse = !$scope.sortReverse;
    $scope.sortBy = sortBy;
  };

  $scope.sortTable = (sortBy, sortReverse = true) =>
    $scope.sortBy === sortBy && $scope.sortReverse === sortReverse;

  $scope.sortAlerts = (alert, sortBy) => {
    switch (sortBy) {
      case 'severity':
        return alert.severity;
      case 'getTimeAgo':
        return alert.getTimeAgo;
      default:
        return alert[sortBy]
    }
  };

  $scope.sortingFn = (alert) => {
    return sortAlerts(alert, $scope.sortBy)
  };

我只是更改了以下内容:

  $scope.sortingFn = (alert) => {
    return sortAlerts(alert, $scope.sortBy)
  };

  const sortingFn = (alert) => {
    return sortAlerts(alert, $scope.sortBy)
  };

现在可以了。没有错误,没有...感谢您的帮助!!

你必须改变 repeat on tr 所以,orderby 影响 tr

<tr ng-repeat="t in Ctrl.notifications | filter : search | 
         filter : Ctrl.customFilter track by $index | 
         orderBy:sortingFn:sortReverse">
    <td>{{t.xxx}}</td>
    <td>{{t.yyyy}}</td>
</tr>