基于数组的ng-repeat过滤

时间:2014-12-01 23:06:24

标签: angularjs ng-repeat

我有一个标准ng-repeat

<tr ng-repeat="tran in filteredTransactions(history)">

$ scope.history正在调用工厂以获取json结果

$scope.history = historyFactory.getHistory;

我创建了另一个范围对象来比较数组中的项目并过滤$scope.history以构建ng-repeat

$scope.filteredTransactions = function () {
    if ($scope.filterBy.length > 1) {
        return $scope.history.filter(function (tran) {
            return $scope.filterBy.indexOf(tran.slot) !== -1;
        });
    }
    else {
        return $scope.history;
    }
}

我有一个&#39;按钮列表&#39;用户可以单击以在筛选器中使用该项目。例如,如果他们点击了第1项&#39;我希望将其添加到数组中并相应地对ng-repeat进行过滤。

我有一个ng-click函数正在推送/弹出数组中的项目。然而,ng-repeat没有更新。有没有更好的方法让它发挥作用?

1 个答案:

答案 0 :(得分:1)

而不是尝试返回过滤项目列表,使用过滤器并将过滤器功能放在那里。

       <tr ng-repeat="tran in history | filter: filteredTransactions(tran)">

        $scope.filteredTransactions = function (item) {
         if ($scope.filterBy.length > 1) {
            // check criteria and return either true or false
         } else {
           return true;
         }
相关问题