【问题标题】:AngularJS: JSON array from FilterAngularJS:来自过滤器的 JSON 数组
【发布时间】:2015-04-10 06:04:58
【问题描述】:

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

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

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

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

如何输出 even 的值?

感谢和问候

【问题讨论】:

标签: arrays angularjs filter


【解决方案1】:

我试过了,效果很好

{{(items | theFilter).even}}

worked plnkr

【讨论】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2016-07-09
      • 1970-01-01
      • 2018-12-24
      • 2013-03-23
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多