AngularJS : Custom filters and ng-repeat

前端 未结 4 1963

I\'m an AngularJS newbie and I\'m building up a small proof-of-concept car hire listings app that pulls in some JSON and renders out various bits of that data via an ng-repe

4条回答
  •  眼角桃花
    2020-11-27 11:36

    If you want to run some custom filter logic you can create a function which takes the array element as an argument and returns true or false based on whether it should be in the search results. Then pass it to the filter instruction just like you do with the search object, for example:

    JS:

    $scope.filterFn = function(car)
    {
        // Do some tests
    
        if(car.carDetails.doors > 2)
        {
            return true; // this will be listed in the results
        }
    
        return false; // otherwise it won't be within the results
    };
    

    HTML:

    ...
    
    ...

    As you can see you can chain many filters together, so adding your custom filter function doesn't force you to remove the previous filter using the search object (they will work together seamlessly).

提交回复
热议问题