我有一個轉發器,我需要由文本輸入到文本字段來過濾,所以這樣做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;
};
您是否嘗試過:'我在filteredItems |過濾:countryQuery'? – theblindprophet
是的,但這將匹配任何字段。例如,我不想將「區域:非洲」與「af」相匹配。只是「alpha_2」或「名稱」字段。 – Steve