繁体   English   中英

AngularJS:来自过滤器的JSON数组

[英]AngularJS: JSON array from Filter

我有一个快速的问题。 我有一个过滤器,我在其中传递一个数组并返回一个数组:

过滤器返回underscore.js库生成的JSON数组:

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

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

例如,如何输出偶数的值?

谢谢和最好的问候

我试过了,效果很好

{{(items | theFilter).even}}

工作的plnkr

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

我认为您可能需要编写另一个过滤器:

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'}}

暂无
暂无

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

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