Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)

前端 未结 13 2568
北恋
北恋 2020-11-22 17:08

Take a look at the example here: http://docs.angularjs.org/api/ng.filter:filter

You can search by any of the phone properties by using

13条回答
  •  旧巷少年郎
    2020-11-22 17:50

    Here is the plunker

    New plunker with cleaner code & where both the query and search list items are case insensitive

    Main idea is create a filter function to achieve this purpose.

    From official doc

    function: A predicate function can be used to write arbitrary filters. The function is called for each element of array. The final result is an array of those elements that the predicate returned true for.

    
    
     
    

    $scope.search = function(item) {
        if (!$scope.query || (item.brand.toLowerCase().indexOf($scope.query) != -1) || (item.model.toLowerCase().indexOf($scope.query.toLowerCase()) != -1) ){
            return true;
        }
        return false;
    };
    

    Update

    Some people might have a concern on performance in real world, which is correct.

    In real world, we probably would do this kinda filter from controller.

    Here is the detail post showing how to do it.

    in short, we add ng-change to input for monitoring new search change

    and then trigger filter function.

提交回复
热议问题