【问题标题】:AngularJs filter with array of values带有值数组的AngularJs过滤器
【发布时间】:2014-10-16 08:34:53
【问题描述】:

是否可以通过将 $scope.items 与数组进行比较来过滤它们?并将结果放入 $scope.itemsfiltered 中? 以下网址说明了一切(向下滚动):http://angular-tips.com/blog/2014/08/tip-accessing-filtered-array-outside-ng-repeat/

我尝试了一个将所有项目与一些数组值进行比较的函数,它可以工作。 但结果仅在我将结果分配给 $scope.items 时显示,而不是在我将其分配给 $scope.itemsfiltered 时显示。

所以A)类似上面的东西(见下面的代码)

或 B) 更简单的东西,一个正则表达式或一些可以比较的东西......所以模型“filtereditem”不是必需的,只需这样做:$scope.filterByRegexOrSomethingElse = /compare1|compare2/;

<div class="item" ng-repeat="item in (itemsfiltered = (items | filter:filterByArr))">
    {{ title }}
</div>

$scope.filterByArray = function () {
    var arr = ['foo', 'bar'];

    // do some magic here
    var filtered = [];

    for (var i = 0; i < items.length; i++) {
        //  resetScope($scope);
        if (_.contains(arr, items[i].categorie)) {
            filtered.push( items[i] );
        }
    }

    $scope.itemsfiltered = filtered;
};

【问题讨论】:

  • 您的问题是什么?如何过滤数组或为什么$scope.itemsfiltered = filtered; 不起作用?
  • 任你选,不管用什么...
  • 在您的代码更改中:item in (itemsfiltered = (items | filter:filterByArr))item in itemsfiltered | filter:filterByArr 并尝试?
  • 对不起,我有点困惑。您是否也尝试使用过滤功能来修改模型?
  • 我将编辑问题,我实际上有一个名为“items”的模型,其中包含这些项目。模型“filtereditems”包含过滤后的模型“items”。以下网址说明了一切(向下滚动):angular-tips.com/blog/2014/08/…

标签: angularjs filter


【解决方案1】:
            $scope.filterCategory = function() {
            if ($(this)[0].category) {
                if ($scope.categories.indexOf($(this)[0].filter.value) == -1) {
                    $scope.categories.push($(this)[0].filter.value);
                }
            } else {
                var indexToRemove = $scope.categories.indexOf($(this)[0].filter.value);
                if (indexToRemove != -1) {
                    $scope.categories.splice(indexToRemove, 1);
                }
            }

            $scope.queryFilter = function(item) {
              return $scope.categories.length ? _.contains($scope.categories, item.categorie) : true;
            };
        };

【讨论】:

    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    相关资源
    最近更新 更多