Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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_Angularjs Ng Repeat - Fatal编程技术网

Javascript 将重复的角度限制为某些行

Javascript 将重复的角度限制为某些行,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,例如,如果我这里有所有书籍语言的json数据集: $scope.data = [{ "title": "Alice in wonderland", "author": "Lewis Carroll", "lang": ["en"] }, { "title": "Journey to the West", "author": "Wu Cheng'en", "lang": ["ch"] }] 我只是想只显示英文书籍,我能用ng repeat中的过滤器

例如,如果我这里有所有书籍语言的json数据集:

$scope.data = [{
    "title": "Alice in wonderland",
    "author": "Lewis Carroll",
    "lang": ["en"]
}, {
    "title": "Journey to the West",
    "author": "Wu Cheng'en",
    "lang": ["ch"]
}]
我只是想只显示英文书籍,我能用ng repeat中的过滤器来完成吗

例如

在HTML中

<div ng-repeat="d in data | MyFilter" style="margin-bottom: 2%">
    {{d.title}}
</div>

{{d.title}}
像这样试试

<div ng-repeat="d in data | filter: { lang : 'en'} " style="margin-bottom: 2%">

您应该使用角度

我还建议您在过滤器中使用一个函数:

<div ng-repeat="item in collection | filter:filteringFunction" style="margin-bottom: 2%">
    {{d.title}}
</div>
现在,如果您愿意,还可以更改comperator函数-
filterByBookLanguage

(我的术语)

假设你的老板突然想让你改变对书籍的过滤逻辑
按作者的名字过滤。现在,您只需添加以下条件:

    if (bossWantsToChangeFilter) {
        $scope.filteringFunction = filterByAuthorName;
    } else {
        $scope.filteringFunction = filterByBookLanguage;
    }
您需要记住的是用当前过滤项编写comperator函数
作为参数,并更新语言/作者姓名的比较值

在您找到的方便位置($scope、local variable、service等)。

感谢您的回复!我的数据集有点错,不是“lang”:“en”更确切地说是“lang”:[“en”]…这似乎与上面的代码不兼容…我的数据集有点错——lang现在是数组,而不是字符串。我试过做collectionItem.lang[0]==“en”,但它似乎没有过滤任何东西…@GwenWong你能提供一个工作的plunker(任何其他类似的东西),还有-当你运行我的comperator函数时,请在if(collectionItem.lang[0]==“en”)条件之前放置一个调试器命令,让我知道您在语言数组的第一个元素中看到了什么:)
<div ng-repeat="item in collection | filter:filteringFunction" style="margin-bottom: 2%">
    {{d.title}}
</div>
    var filteredLang = "en";

    function filterByBookLanguage(collectionItem) {
        var result = false;

        if (collectionItem.lang[0] === filteredLang) {
            result = true;
        }

        return result;
    }

    $scope.filteringFunction = filterByBookLanguage;
    if (bossWantsToChangeFilter) {
        $scope.filteringFunction = filterByAuthorName;
    } else {
        $scope.filteringFunction = filterByBookLanguage;
    }