【问题标题】:Angularjs and filterAngularjs 和过滤器
【发布时间】:2013-08-28 05:17:01
【问题描述】:

我有一本字典:

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

如何使用 angularjs 按值过滤这些数据?我需要得到这个(只有正确的):

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

【问题讨论】:

  • 在视图或控制器中?
  • 你能澄清一下用法吗?如果它在ngRepeatngOptions 中(ngFilter 可以工作的任何地方),您可以循环 (key, value) 并使用filter: value!见ngFilter
  • 我有复选框列表,我需要获取所有选中复选框的列表。 <div ng-repeat="box in boxes" > <input type="checkbox" ng-model="boxdict[box.id]" value="{$ box.id $}" ng-show="checkboxes"></div>
  • 然后我的评论中的ngFilter 应该可以,试试吧!
  • 你不应该在对象中使用数字和键。这可能会导致一些问题。也许对象数组会更清楚。

标签: javascript angularjs filtering


【解决方案1】:

听起来您正在尝试获取代码方面的列表,而不是模板。

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 放入 $scope$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;
}

【讨论】:

    【解决方案2】:
    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: &ltli ng-repeat='c in getValues(item)'&gt {{C}} &ltli&gt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多