Javascript 或带有角JS复选框

Javascript 或带有角JS复选框,javascript,angularjs,checkbox,Javascript,Angularjs,Checkbox,我使用多个复选框来使用angularjs过滤属性。目前,我正在使用自定义过滤器来显示特定类型的所有属性。我有多个复选框,当您选中每个新复选框时,它会过滤结果 目前,您选中的每个新复选框都会缩小您的搜索范围(即哪些房产是农村和沿海房产),我希望扩大搜索范围(即哪些房产是农村或沿海房产)。我对这真的很陌生 这是我的应用程序: propertyApp.controller('PropertyListControl', function ($scope) { $scope.properties = [

我使用多个复选框来使用angularjs过滤属性。目前,我正在使用自定义过滤器来显示特定类型的所有属性。我有多个复选框,当您选中每个新复选框时,它会过滤结果

目前,您选中的每个新复选框都会缩小您的搜索范围(即哪些房产是农村和沿海房产),我希望扩大搜索范围(即哪些房产是农村或沿海房产)。我对这真的很陌生

这是我的应用程序:

propertyApp.controller('PropertyListControl', function ($scope) {
$scope.properties = [
    {
        title: "Sharrow Bay Hotel",
        location:['rural', 'coastal']
    },
    {
        title: "The Royal Oak Inn",
        location:['rural']
    },
    {
        title: "Scale Hill Cottages",
        location:['urban']
    },
];

$location = {}

// Currently using this great custom filter:
}).filter('filteredLocation', function() {
    return function(properties, location) {
        var result = properties.slice(); // copy array
            angular.forEach(location, function(value, key) {
                if(value) {
                    for(var index = 0; index < result.length; index++) {
                        property = result[index];
                            if(property.location.indexOf(key) == -1) {
                            result.splice(index--,1);
                            }
                        }
                    }
                });
                return result;
            };
        });
propertyApp.controller('PropertyListControl',函数($scope){
$scope.properties=[
{
标题:“沙罗湾酒店”,
地点:[“农村”、“沿海”]
},
{
标题:“皇家橡树酒店”,
地点:[“农村”]
},
{
标题:“规模山别墅”,
地点:[“市区”]
},
];
$location={}
//当前正在使用此伟大的自定义过滤器:
}).filter('filteredLocation',function(){
返回函数(属性、位置){
var result=properties.slice();//复制数组
角度。forEach(位置、功能(值、键){
如果(值){
对于(var index=0;index
以及我的复选框:

<label><input type="checkbox" ng-model="location.rural"/>Rural</label>
<label><input type="checkbox" ng-model="location.urban"/>Urban</label>
<label><input type="checkbox" ng-model="location.coastal"/>Coastal</label>
农村
城市的
沿海的

过滤器从您的所有位置开始:

var result = properties.slice();
并删除任何与测试不匹配的内容:

result.splice(index--,1);
因此,它的行为就像一个“和”,因为,在你的例子中,没有“沿海”的任何东西被删除,然后没有“农村”的任何东西被删除。因此,只剩下符合这两个条件的项目

要将其转换为“或”过滤器,我将从一个空数组开始:

var result = []; 
并添加匹配的结果(因此将添加与任一测试匹配的任何结果):

为了避免重复,我还切换了循环,因此外部循环现在覆盖属性列表,而内部循环覆盖要过滤的位置列表。然后,一旦发现属性与任何位置匹配,就可以中止内部循环

以下是整个功能:

.filter('filteredLocation', function() {
    return function(properties, location) {
      var result = []; 
      for(var index = 0; index < properties.length; index++) {  
         var added = false;
         angular.forEach(location, function(value, key) {         
           if(value && !added) {
              property = properties[index];
              if(property.location.indexOf(key) != -1) {
                 result.push(property);  
                 added = true; // Mark as added so we don't add duplicates
              }       
          }
       })
   };
   return result;
};
.filter('filteredLocation',function(){
返回函数(属性、位置){
var结果=[];
对于(var index=0;index

在plunker中创建一个演示,当我们可以访问代码和控制台时,帮助会更容易
.filter('filteredLocation', function() {
    return function(properties, location) {
      var result = []; 
      for(var index = 0; index < properties.length; index++) {  
         var added = false;
         angular.forEach(location, function(value, key) {         
           if(value && !added) {
              property = properties[index];
              if(property.location.indexOf(key) != -1) {
                 result.push(property);  
                 added = true; // Mark as added so we don't add duplicates
              }       
          }
       })
   };
   return result;
};