Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组中多个字符串的角度滤波器_Javascript_Angularjs_Angular Filters - Fatal编程技术网

Javascript 数组中多个字符串的角度滤波器

Javascript 数组中多个字符串的角度滤波器,javascript,angularjs,angular-filters,Javascript,Angularjs,Angular Filters,问题是,对具有2个类别的项目进行筛选不会返回任何内容。所以,通过过滤“mulder”,应该返回类别为“mulder”和“scully”的项目 我有定义以下内容的数据: $scope.allnodes = [ { name: "This is node 1", category: ["mulder"] }, { name: "Another title is this one", category: ["scully"] }, { name: "This is Another title", ca

问题是,对具有2个类别的项目进行筛选不会返回任何内容。所以,通过过滤“mulder”,应该返回类别为“mulder”和“scully”的项目

我有定义以下内容的数据:

$scope.allnodes = [
{ name: "This is node 1", category: ["mulder"] },
{ name: "Another title is this one", category: ["scully"] },
{ name: "This is Another title", category: ["mulder"] },
{ name: "And finally a fourth title", category:  ["mulder","scully"] }      
];
相关应用程序js为:

$scope.categories = ['Mulder','Scully'];
$scope.filterByCategory = function (node) {
    return $scope.filter[node.category] || noFilter($scope.filter);
};


function noFilter(filterObj) {
    for (var key in filterObj) {
        if (filterObj[key]) {
            return false;
            }
        }
    return true;
}  
这是部分渲染:

<div ng-controller="myCtrl">
    <b>Category:</b>
    <div ng-repeat="cat in categories">
        <b><input type="checkbox" ng-model="filter[cat]" />{{cat}} 
        </b>
    </div>
    <hr />
    <div ng-repeat="node in filtered=(allnodes | filter:filterByCategory)">
    {{node.title}} 
        <div ng-repeat="term in node.category">
            <label class="label label-info">{{term}}</label>
        </div> 
    </div>
    <hr />
    Number of results: {{filtered.length}}
</div>

类别:
{{cat}}

{{node.title} {{term}}
结果数:{filtered.length}

问题在于node.category是一个数组

您的filterByCategory应该如下所示:

$scope.filterByCategory = function(node) {
    for (var key in $scope.filter) {
      if ($scope.filter[key] && (!node.category || node.category.indexOf(key) < 0)) {
        return false;
      }
    }

    return true;
};
$scope.filterByCategory=函数(节点){
for(变量键在$scope.filter中){
if($scope.filter[key]&&(!node.category | | node.category.indexOf(key)<0)){
返回false;
}
}
返回true;
};
此函数用于检查当前项目的类别是否包含过滤器中的所有选定类别


谢谢你,坎皮,但我还是没能让它工作。。。我已经将其粘贴到了一个提琴中,如果这样更容易看到的话:提琴中的代码与原始代码有点不同。我更新了我的答案,以支持没有类别和大写类别名称的项目。感谢您的持续帮助bumpy,您的小提琴链接工作得更好,但是勾选两个类别不会返回包含两个类别的项目,我认为它应该返回。您确定吗?对我来说,也许我的解释并不理想,这是我在一张图片中的意思,这应该显示3个结果:“穆德”“史高丽”和“穆德,史高丽”