0

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!

1
  • I'm using jade templating, renders to: <tr class="pointer" ng-repeat="asset in completed | dateRange" ng-animate="'animate'" ng-click="selectAsset(asset);"</tr> <td>{{asset.Name}}</td> Commented Jan 23, 2016 at 1:09

2 Answers 2

2

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
             });
        }            
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

When you define a custom filter function, the value passed into the filter is replaced by the value returned from the filter, so you are replacing item with true.

For a filter which logs the input without changing it, just return the input:

return function(item) {
    console.log(item);

    return item;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.