简体   繁体   English

AngularJS筛选器无法正常工作

[英]AngularJS Filter does not work well

I have an array ($scope.names) that contains some simple objects which I am applying some functions to clean the value from diacritics. 我有一个数组($ scope.names),其中包含一些简单的对象,我正在应用一些函数来清除变音符号中的值。 When I print with console this value it shows me ok. 当我使用控制台打印此值时,它显示正常。 For example: 'La 45 km nord- vest de Bucureşti' it will be 'La 45 km nord- vest de Bucuresti'. 例如:“ La 45 km nord-背心deBucureşti”将是“ La 45 km nord-背心de Bucuresti”。

I am applying this method and for another variable ($scope.searchText) to make the searchable compatible. 我正在应用此方法和另一个变量($ scope.searchText)以使可搜索兼容。 Again, it's fine. 再次,很好。

The problem is that even I am standardizing that two variables value, the filter it doesn't work. 问题是,即使我正在标准化两个变量的值,它也不起作用的过滤器。 If I try to search for 'Bucures' it wont show me the object that contains the clean word 'Bucures' (original word 'Bucureş')... 如果我尝试搜索“ Bucures”,它不会告诉我包含干净单词“ Bucures”(原始单词“Bucureş”)的对象...

What I have doing wrong? 我做错了什么?

The full code is here: http://plnkr.co/edit/D5WfiOGd5I3QfcNVdOmr?p=preview 完整的代码在这里: http : //plnkr.co/edit/D5WfiOGd5I3QfcNVdOmr?p=preview

The angular script is that: 角度脚本是:

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);
    });
});

I have made some changes to the filter code ` 我对过滤器代码做了一些更改

$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);
    });

` `

Check this Plunk 检查这个Plunk

The answer it was using the 'function (actual, expected)' to return a boolean value: 答案是使用“函数(实际的,预期的)”返回布尔值:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM