0

我有一个转发器,我需要由文本输入到文本字段来过滤,所以这样做AngularJS滤波器中继器具有多个值

<tr ng-repeat="i in filteredItems = (iso3166 | filter: {alpha_2: countryQuery})">

的数据是对象的JSON数组, $ scope.iso3166:

[{ 
    "name": "Afghanistan", 
    "alpha_2": "AF", 
    "alpha_3": "AFG", 
    "country-code": "004", 
    "iso_3166-2": "ISO 3166-2:AF", 
    "region": "Asia", 
    "sub-region": "Southern Asia", 
    "region-code": "142", 
    "sub-region-code": "034", 
    "license": "unclassified", 
    "prohibited": "unclassified", 
    "size": "unclassified" 
}, ... 

所以你可以键入“af”和表格过滤器来显示阿富汗。

现在我需要输入什么输入到字段返回匹配,而不仅仅是针对alpha_2键,而是name。例如,“af”不仅应该匹配“阿富汗”,还应匹配“中非共和国”。

我看着Angular 1.4.1文档,看到了逗号方法,但它似乎执行AND比较。作为

<tr ng-repeat="i in filteredItems = (iso3166 | filter: {alpha_2: countryQuery, name: countryQuery })">

有没有办法不执行“或”在这种情况下,这样无论是输入过滤器,其中查询是“alpha_2” “名”的任何项目?

更新:如果有人好奇,我结束了使用过滤器,如下面的回答表明:

$scope.countryFilter = function (value) { 
    if (angular.isUndefined($scope.countryQuery) || $scope.countryQuery === '') { 
     return true; 
    } 
    return value.name.indexOf($scope.countryQuery) >= 0 || value.alpha_2.indexOf($scope.countryQuery) >= 0; 
    }; 
+0

您是否尝试过:'我在filteredItems |过滤:countryQuery'? – theblindprophet

+0

是的,但这将匹配任何字段。例如,我不想将“区域:非洲”与“af”相匹配。只是“alpha_2”或“名称”字段。 – Steve

回答

1

而是做这种方式,你可以指定一个函数的名称来筛选结果然后在您的控制器中实施过滤逻辑。

<tr ng-repeat="i in filteredItems = (iso3166 | filter: filterFn)"> 

在控制器:

scope.filterFn = function(item) { 
    if(//item meets criteria) { 
     //returning true will put the item into the ng-repeat data 
     return true; 
    } 
    return false; 
} 
+0

我结束了写一个过滤器。我仍然喜欢在html中做这件事,但速度更快。 – Steve

0

您可以将属性这样的NG-重复过滤:

https://jsfiddle.net/pzek3tmy/

控制器

function Controller($scope) { 
     var vm = this; 

     vm.regions = [{ 
     "name": "Afghanistan", 
     "alpha_2": "AF", 
     "alpha_3": "AFG", 
     "country-code": "004", 
     "iso_3166-2": "ISO 3166-2:AF", 
     "region": "Asia", 
     "sub-region": "Southern Asia", 
     "region-code": "142", 
     "sub-region-code": "034", 
     "license": "unclassified", 
     "prohibited": "unclassified", 
     "size": "unclassified" 
     }, { 
     "name": "Germany", 
     "alpha_2": "GN", 
     "alpha_3": "AFG", 
     "country-code": "004", 
     "iso_3166-2": "ISO 3166-2:AF", 
     "region": "Asia", 
     "sub-region": "Europe", 
     "region-code": "143", 
     "sub-region-code": "034", 
     "license": "unclassified", 
     "prohibited": "unclassified", 
     "size": "unclassified" 
     }]; 
    } 

HTML

<div ng-controller="Controller as ctrl"> 
    <input type="text" ng-model="region_filter"> 
    <ul> 
    <li ng-repeat="region in ctrl.regions | filter:{'alpha_2':region_filter} | filter:{'name':region_filter}">{{region.name}}  </li> 
    </ul> 
</div> 
+0

这只会返回匹配两个(AND)的匹配项,问题是如何匹配(OR)。在你的例子中,“德国”不会返回德国的结果。 –