[英]AngularJS Filter does not work well
我有一個數組($ scope.names),其中包含一些簡單的對象,我正在應用一些函數來清除變音符號中的值。 當我使用控制台打印此值時,它顯示正常。 例如:“ La 45 km nord-背心deBucureşti”將是“ La 45 km nord-背心de Bucuresti”。
我正在應用此方法和另一個變量($ scope.searchText)以使可搜索兼容。 再次,很好。
問題是,即使我正在標准化兩個變量的值,它也不起作用的過濾器。 如果我嘗試搜索“ Bucures”,它不會告訴我包含干凈單詞“ Bucures”(原始單詞“Bucureş”)的對象...
我做錯了什么?
完整的代碼在這里: http : //plnkr.co/edit/D5WfiOGd5I3QfcNVdOmr?p=preview
角度腳本是:
var app = angular.module('myApp', []);
app.controller('namesCtrl', function($scope, $filter) {
$scope.names = [
{
"adresa": "Str. Calafatului nr. 10",
"latitudine": "44.1",
"localitatea": "GALICEA MARE",
"longitudine": "23.3",
},
{
"adresa": "bd. Elisabeta nr. 1",
"latitudine": "44.170901",
"localitatea": "CONSTANŢA",
"longitudine": "28.663195",
},
{
"POTLOGI": "La 45 km nord- vest de Bucureşti",
"latitudine": "44.564319",
"localitatea": "POTLOGI",
"longitudine": "25.588091",
}
]
$scope.searchText = "";
$scope.searchCleanText = "";
$scope.myFilter = function (items) {
for (var i = 0; i < items.length; i++) {
var boolChk = false;
angular.forEach(items[i], function (value, key) {
var cleanValue = removeDiacritics(value).toLowerCase();
if (boolChk === false) {
boolChk = cleanValue.includes($scope.searchCleanText);
}
console.log(value + ' ' + cleanValue.includes($scope.searchCleanText) + '\tboolChk = ' + boolChk);
return boolChk;
});
}
}
$scope.$watch('searchText', function() {
$scope.searchCleanText = removeDiacritics($scope.searchText).toLowerCase();
$scope.showItems = $filter('filter')($scope.names, $scope.searchText, $scope.myFilter($scope.names));
console.log($scope.showItems);
});
});
我對過濾器代碼做了一些更改
$scope.myFilter = function (item) {
//for (var i = 0; i < items.length; i++) {
var boolChk = false;
angular.forEach(item, function (value, key) {
var cleanValue = removeDiacritics(value).toLowerCase();
if (boolChk === false) {
boolChk = cleanValue.includes($scope.searchCleanText);
}
console.log(value + ' ' + cleanValue.includes($scope.searchCleanText) + '\tboolChk = ' + boolChk);
return boolChk;
});
return boolChk;
//}
}
$scope.$watch('searchText', function() {
$scope.searchCleanText = removeDiacritics($scope.searchText).toLowerCase();
$scope.showItems = $filter('filter')($scope.names, $scope.searchText, function(actual,expected){return $scope.myFilter(actual);});
console.log($scope.showItems);
});
`
檢查這個Plunk
答案是使用“函數(實際的,預期的)”返回布爾值:
$scope.functionFilter = function (actual, expected) {
if (typeof actual === 'object') {
var obj = actual;
//console.log(obj);
var boolChk = false;
angular.forEach(obj, function (value, key) {
var cleanValue = removeDiacritics(value).toLowerCase();
if (boolChk === false) {
boolChk = cleanValue.includes($scope.searchCleanText);
}
});
console.log(obj);
console.log(boolChk);
return boolChk;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.