angularjs使用OR条件

时间:2015-10-16 15:34:13

标签: angularjs

我尝试使用针对特定字段的角度过滤器进行搜索字段过滤,这应该是OR条件

这是演示实现http://jsfiddle.net/tomalex0/ck19wvdx/2/。 我尝试仅在NameGender上搜索,而不是Location

无论我尝试什么组合,它都更像AND条件。在OR condition中是否有内置方式filter

<div ng-app="myApp" ng-controller="myController">
<input type="text" ng-model="search">
<ul>
    <li ng-repeat="user in users |  filter: search">{{user.Name}} | {{user.Location}}
    </li>
</ul>
</div>





var data = [
        {
            Name: "John Smith",
            Gender: "M",
            Location: "Wales"
        },
        {
            Name: "Sally Smith",
            Gender: "F",
            Location: "USA"
        },
        {
            Name: "Curtis Timson",
            Gender: "M",
            Location: "England"
        },
        {
            Name: "Sam Wallace",
            Gender: "M",
            Location: "England"
        },
        {
            Name: "Caroline James",
            Gender: "F",
            Location: "Scotland"
        }
    ];

    var myApp = angular.module("myApp", []);

    myApp.controller("myController", function($scope){
        $scope.users = data;
    });

2 个答案:

答案 0 :(得分:1)

您可能需要使用过滤器功能,而不是直接将搜索字符串传递给过滤器。相关代码如下。 Here is ur fiddle modified.

<li ng-repeat="user in users |  filter: filter">

$scope.filter = function(item) {
 var regex = new RegExp($scope.search, 'gi');
 return item.Name.match(regex) || item.Gender.match(regex); 
};

答案 1 :(得分:0)

您的演示对我来说就像OR运算符一样。

例如,如果我在输入中写sa,结果是:

Sally Smith | USA
Sam Wallace | England

使用ANDSam Wallace | England将被隐藏,因为England不包含sa