Search code examples
javascriptangularjsangularjs-filterangularjs-module

angularJS global filter module


I am having trouble getting my custom filter to work.

I have a module of global filters:

angular.module('globalFilters', []).filter('dateRange', function () {
    return function(item) {
        console.log(item);
        return true;
    }
}); 

This is injected into my app on creation. I am trying to apply this to an ng-repeat:

tr.pointer(ng-repeat="asset in completed | dateRange", ng-animate="'animate'", ng-click="selectAsset(asset);")
    td {{asset.Name}}

However adding this filter will filter all assets out of table. To try to isolate the problem I am returning true for the function to display all assets but it is not working.

Upon logging item to the console, result appears to be an array of all assets so I guess something is wrong.

I was following this tutorial https://docs.angularjs.org/tutorial/step_09

Thanks!


Solution

  • You are filtering an array...so your filter function needs to return an array.

    .filter('dateRange', function () {
        return function(itemArray) {
            if(!itemArray){
                return null
            }else{
                 return itemArray.filter(function(item){
                     // conditions of filter
                 });
            }            
        }
    });