简体   繁体   English

angularJS全局过滤器模块

[英]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: 我正在尝试将此应用于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. 但是,添加此过滤器将过滤掉表中的所有assets To try to isolate the problem I am returning true for the function to display all assets but it is not working. 为了尝试找出问题,我返回true以使该函数显示所有assets但无法正常工作。

Upon logging item to the console, result appears to be an array of all assets so I guess something is wrong. item记录到控制台后,结果似乎是所有assets的数组,因此我想有些问题。

I was following this tutorial https://docs.angularjs.org/tutorial/step_09 我正在关注本教程https://docs.angularjs.org/tutorial/step_09

Thanks! 谢谢!

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
             });
        }            
    }
});

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 . 定义自定义过滤器功能时,传递给过滤器的值将替换为从过滤器返回的值,因此您将true替换为item

For a filter which logs the input without changing it, just return the input: 对于记录输入而不更改输入的过滤器,只需返回输入:

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

    return item;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM