AngularJS:来自Filter的JSON数组

时间:2015-04-10 06:04:58

标签: arrays angularjs filter

我有一个简单的问题。我有一个过滤器,我传递一个数组并返回一个数组:

Filter返回由underscore.js库生成的JSON数组:

myApp.filter('theFilter', function () {
        return function(items){
            return _.countBy(items, function(num) {
              return num % 2 == 0 ? 'even': 'odd';
            });
        };
});

这个{{array | theFilter }}只输出一个像这样的json数组:{{“even”:3,“odd”:5}}

如何输出偶数值?

谢谢和最好的问候

3 个答案:

答案 0 :(得分:1)

我试过,它完美无缺

{{(items | theFilter).even}}

worked plnkr

答案 1 :(得分:0)

myApp.filter('theFilter', function () {
    return function(items){
        return _.countBy(items, function(num) {
          return num % 2 == 0 ? 'even': 'odd';
        }).even;
    };
});

答案 2 :(得分:0)

我想也许你需要写另一个过滤器:

myApp.filter('theFilter', function () {
        return function(items){
            return _.countBy(items, function(num) {
              return num % 2 == 0 ? 'even': 'odd';
            });
        };
});

myApp.filter('attr', function () {
        return function(obj, attrName){
            return obj ? obj[attrName] : undefined;
        };
});

{{array | theFilter | attr:'even'}}