AngularJs 中内置过滤器的 OR 逻辑
OR logic on builtin filters in AngularJs
我有一个看起来像这样的对象:
{HospitalName: "hospital name", DoctorDetails: {Name: "doctor name"}}
我正在使用 ng-repeat 遍历此类对象的列表。同时,我正在应用两个具有 OR 逻辑的过滤器,这样至少其中一个应该 return 匹配(如果有)。
<input type="text" ng-model="searchQuery" placeholder="Search by hospital or doctor name">
<div ng-repeat="hospital in list | filter: {HospitalName : searchQuery} || {DoctorDetails : {Name : searchQuery}}">
<div>{{hospital.HospitalName}}</div>
</div>
但是,只有第一个过滤器被触发。即使有些对象的医生姓名与搜索查询相匹配,也没有匹配项 returned。我哪里出错了?
我使用这个 post 作为过滤器条件逻辑的参考:
更新:
我无法弄清楚为什么条件逻辑不起作用。因此,根据@Walfrat 的建议,我不得不求助于使用自定义过滤器,它完成了工作。如果有人感兴趣,这是我使用的代码:
angular.module('TestApp')
.filter('searchBox', function($filter, $rootScope) {
return function(input, searchParam) {
if(input) {
var searchResults = null;
//check if a valid search query has been entered
if(searchParam){
//use $filter to find hospital names that match query
searchResults = $filter('filter')(input,{HospitalName: searchParam});
//if no hospital names match, fill searchResults with doctor name results(if any) that match query
//otherwise just append the results to searchResults
if(searchResults.length == 0){
searchResults = $filter('filter')(input,{DoctorDetails : {Name: searchParam}});
}
else{
angular.forEach($filter('filter')(input,{DoctorDetails : {Name: searchParam}}), function(result){
searchResults.push(result);
});
}
//if there are no doctor or hospital names that match,
// set searchResult to approprriate message to denote no results found
if(searchResults.length == 0){
searchResults = ["Nothing"];
}
return searchResults;
}
//console.log(out);
return input;
}
return [];
}
});
更简洁的代码,请参见@Alex Chance 的回答。干杯!
在您的情况下,应用自定义过滤器功能可能更容易。
您像这样在 ng-repeat 中应用过滤器
<div ng-repeat="hospital in list | filter:filterFn ">
过滤器函数如下所示:
$scope.filterFn = function(hospital)
{
return hospital.HospitalName.search($scope.searchQuery) != -1 ||
hospital.DoctorDetails.Name.search($scope.searchQuery) != -1;
}
这是一个工作 fiddle。
我有一个看起来像这样的对象:
{HospitalName: "hospital name", DoctorDetails: {Name: "doctor name"}}
我正在使用 ng-repeat 遍历此类对象的列表。同时,我正在应用两个具有 OR 逻辑的过滤器,这样至少其中一个应该 return 匹配(如果有)。
<input type="text" ng-model="searchQuery" placeholder="Search by hospital or doctor name">
<div ng-repeat="hospital in list | filter: {HospitalName : searchQuery} || {DoctorDetails : {Name : searchQuery}}">
<div>{{hospital.HospitalName}}</div>
</div>
但是,只有第一个过滤器被触发。即使有些对象的医生姓名与搜索查询相匹配,也没有匹配项 returned。我哪里出错了?
我使用这个 post 作为过滤器条件逻辑的参考:
更新:
我无法弄清楚为什么条件逻辑不起作用。因此,根据@Walfrat 的建议,我不得不求助于使用自定义过滤器,它完成了工作。如果有人感兴趣,这是我使用的代码:
angular.module('TestApp')
.filter('searchBox', function($filter, $rootScope) {
return function(input, searchParam) {
if(input) {
var searchResults = null;
//check if a valid search query has been entered
if(searchParam){
//use $filter to find hospital names that match query
searchResults = $filter('filter')(input,{HospitalName: searchParam});
//if no hospital names match, fill searchResults with doctor name results(if any) that match query
//otherwise just append the results to searchResults
if(searchResults.length == 0){
searchResults = $filter('filter')(input,{DoctorDetails : {Name: searchParam}});
}
else{
angular.forEach($filter('filter')(input,{DoctorDetails : {Name: searchParam}}), function(result){
searchResults.push(result);
});
}
//if there are no doctor or hospital names that match,
// set searchResult to approprriate message to denote no results found
if(searchResults.length == 0){
searchResults = ["Nothing"];
}
return searchResults;
}
//console.log(out);
return input;
}
return [];
}
});
更简洁的代码,请参见@Alex Chance 的回答。干杯!
在您的情况下,应用自定义过滤器功能可能更容易。
您像这样在 ng-repeat 中应用过滤器
<div ng-repeat="hospital in list | filter:filterFn ">
过滤器函数如下所示:
$scope.filterFn = function(hospital)
{
return hospital.HospitalName.search($scope.searchQuery) != -1 ||
hospital.DoctorDetails.Name.search($scope.searchQuery) != -1;
}
这是一个工作 fiddle。