簡體   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