0

I have a dictionary:

{ "69": true, "72": true, "97": true, "113": false, "136": true, "189": false, "396": true, "404": false, "454": false }

How i can filter this data by value using angularjs? I need to get this (Only true):

{ "69": true, "72": true, "97": true, "136": true,  "396": true}
4

2 回答 2

0
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
    $scope.item = { "69": true, "72": true, "97": true, "113": false, "136": true,
    "189":false, "396": true, "404": false, "454": false }
    $scope.getValues = function(item)
    {
        var ary = [];
        for(var key in item){
            if(item[key])
                 ary.push(key);
    }
    return ary;
});
   
html:
    <li ng-repeat='c in getValues(item)'>
     {{C}}
    <li>
于 2013-11-07T08:55:03.297 回答
0

听起来您正在尝试在代码端而不是模板上获取列表。

var checked = {};
for(var key in $scope.boxes){
    if($scope.boxes[key])
        checked[key] = true; // true or $scope.boxes[key], which in your case will be true
}
// checked will be { "69": true, "72": true, "97": true, "136": true,  "396": true}

根据您是否需要在每次检查时不断更新此值,您可能需要checked输入$scopeand $watch $scope.boxes,为每次更改运行此值。

如果您只需要在做某事之前检查过的内容,那么最好在您做某事之前找到检查过的内容。只是传递$scope.boxes给这个:

function findChecked(obj){
    var checked = {};
    for(var key in obj){
        if(obj[key])
            checked[key] = true; // true or obj[key], which in your case will be true
    }
    return checked;
}
于 2013-08-26T03:54:28.887 回答