angularjs filtering multiple expressions

后端 未结 1 922
粉色の甜心
粉色の甜心 2020-12-11 21:37

I am new to angularjs and just wanted some help with filtering...

I understand the basics of filtering (which is very basic) but what I want to do is filter using mu

相关标签:
1条回答
  • 2020-12-11 22:40

    At least you can't simply use the basic form of the filter filter, because AngularJS can't guess alone that a space mean "or".

    But you don't need to create your own directive. AngularJS 1.1.5 provides a third parameter for filter (see the documentation). It can take a function, which "will be given the object value and the predicate value to compare and should return true if the item should be included in filtered result". Then, all you have to do is:

    $scope.multipleChoicesComparator = function (expected, actualInCompactForm)
    {
        // Get a list of predicates
        var listActual = actualInCompactForm.split(' ');
        var mustBeIncluded = false;
    
        angular.forEach(listActual, function (actual)
        {
            // Search for a substring in the object value which match
            // one of the predicate, in a case-insensitive manner
            if (angular.lowercase(expected).indexOf(angular.lowercase(actual)) !== -1)
            {
                mustBeIncluded = true;
            }
        });
    
        return mustBeIncluded;
    };
    
    <li data-ng-repeat="phone in phones | filter:phoneFilter:multipleChoicesComparator">
        {{phone.make}} {{phone.model}} 
    </li>
    

    Fiddle

    0 讨论(0)
提交回复
热议问题