我在使用两个带有ng-hide的过滤器时遇到了麻烦。 这是一个显示我的问题的小提琴:http://jsfiddle.net/czPf6/2/ 这是我的代码,根据输入框和选择框中的输入值有条件地选择要应用的过滤器:
function applySearchFilter() {
var filterCategory = $scope.filters.cat.toLowerCase();
//console.log(filterCategory);
var category = $scope.friend.job.toLowerCase();
//console.log(category);
var filter = $scope.filters.name.toLowerCase();
//console.log(filter);
var name = $scope.friend.name.toLowerCase();
console.log(name + " " + category);
console.log("filter:" + filter + "/category:" + filterCategory);
if($scope.filters.name == "" && $scope.filters.cat !== ""){
var isCategory = (filterCategory == category);
console.log(isCategory + "1st");
$scope.isExcludedByFilter = ! isCategory;
console.log($scope.isExcludedByFilter);
} else if($scope.filters.name !=="" && $scope.filters.cat == ""){
var isSubstring = ( name.indexOf( filter ) !== -1 );
console.log(isSubstring + "2nd");
$scope.isExcludedByFilter = ! isSubstring;
console.log($scope.isExcludedByFilter);
} else if($scope.filters.name !=="" && $scope.filters.cat !==""){
var isCategory = (filterCategory == category);
var isSubstring = ( name.indexOf( filter ) !== -1 );
console.log(isCategory + "3rd");
console.log(isSubstring + "3rd");
$scope.isExcludedByFilter = ! isSubstring && ! isCategory;
console.log($scope.isExcludedByFilter);
}
日志语句打印出我期望它们的内容,但过滤器仅在两个输入字段都有值时才有效。我在这里做错了什么?
答案 0 :(得分:1)
最后导致它的是你的isExcludedByFilter:
$scope.isExcludedByFilter = ! isSubstring || ! isCategory;
我评论说排队了。
我改变了:
} else if($scope.filters.name !=="" && $scope.filters.cat == ""){
var isSubstring = ( name.indexOf( filter ) !== -1 );
$scope.isExcludedByFilter = ! isSubstring && ! isCategory; // added && !isCategory
} else if($scope.filters.name !=="" && $scope.filters.cat !==""){
var isCategory = (filterCategory == category);
var isSubstring = ( name.indexOf( filter ) !== -1 );
$scope.isExcludedByFilter = ! isSubstring || ! isCategory; // changed && to ||
}
我还在“Any”过滤器的上述if语句的末尾添加了以下内容。这意味着它将在列表中显示所有“朋友”:
} else if($scope.filters.name =="" && $scope.filters.cat == ""){
$scope.isExcludedByFilter = false;
}