如何有条件地改变角度的orderBy参数?

时间:2015-04-07 14:14:43

标签: javascript html angularjs

我有一个带有orderBy参数的表行。如何根据方法返回的值将其更改为其他参数?

这就是我的tr看起来 -

<tr class="pl" ng-repeat="thing in things | orderBy:'oldId':reverse" ng-class="{'row-error': component.missing}">
          <td class="drag-handle" sortable-handle><i class="move"></i></td>
          <td class="some-checkbox delete">
            <div class="cb-wrapper">
                ...
            </div>
          </td>
          <td class="some-type">
            <i type-icon="component.type" component-url="component.url"/>
          </td>
        </tr>

这里,根据isFeatureAvailable()方法返回的值,我希望orderBy为

orderBy:'oldOrder':reverse"

orderBy:'newOrder'

我该怎么做?

2 个答案:

答案 0 :(得分:2)

'oldOrder'和'newOrder'可以在范围变量上,该变量基于其他逻辑而变化。

e.g. $scope.sortCriteria = 'oldOrder';

然后在其他地方:

$scope.changeCriteriaToNew = function(){
    $scope.sortCriteria = 'newOrder';
}

并在html中:

ng-repeat="thing in things | orderBy:sortCriteria:reverse"

SethGunnells的方式也是另一种方法。

答案 1 :(得分:1)

这样的事情应该可以解决问题。

function order() {
  if (isFeatureAvailable() === 'something') return '-oldOrder';
  return 'newOrder';
}

然后只需通过控制器公开该功能,并在指令中使用它,如下所示:

ng-repeat="thing in things | orderBy: order()"

这是JS Bin中的live example