我有一个数组($ 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

10-06 15:57