【问题标题】:AngularJS ng-repeat filtering with an OR-statement使用 OR 语句的 AngularJS ng-repeat 过滤
【发布时间】:2013-08-19 16:32:44
【问题描述】:

所以我有一个存储在控制器变量中的公司数据集。

$scope.companies = [
    {
        name: "first company",
        type: ["a", "b"]
    },
    {
        name: "second company",
        type: ["b"]
    },
    {   
        name: "third company",
        type: ["c"]
    }
]

然后我有我的列表,我想列出 a 或 b 的所有公司。我认为发送和数组将作为或语句工作。事实证明它更像是一个 and 语句。

<ul>
    <li ng-repeat="company in companies | filter:filters{type: ['a', 'b']}">
        {{company.name}}
    </li>
</ul>

上面的代码将列出“第一家公司”,而我希望它同时列出“第一家公司”和“第二家公司”。我知道我可以从控制器中操作 $scope.companies 数据集,但我想知道是否有任何“本地”方法可以首先实现这一点。

最好的问候! // 理查德

【问题讨论】:

    标签: angularjs angularjs-ng-repeat angularjs-filter


    【解决方案1】:

    您唯一的选择是通过函数使用过滤器:

    <ul>
        <li ng-repeat="company in companies | filter:customFunction">
            {{company.name}}
        </li>
    </ul>
    

    在哪里

    $scope.customFunction = function(item){ /* true if you want item, false if not */ }
    

    来源:http://docs.angularjs.org/api/ng.filter:filter#parameters

    【讨论】:

    • 谢谢!比数据集的排列更优雅。也感谢来源!
    • 谢谢! @RichardLilja 你为什么不验证这个答案是你问题的解决方案?它使它在搜索引擎中与您的问题匹配的搜索排名更高,可以帮助我在浪费时间制作自定义过滤器之前找到这个优雅的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多