如何筛选(键,值)在AngularJs NG重复? [英] How to filter (key, value) with ng-repeat in AngularJs?

查看:292
本文介绍了如何筛选(键,值)在AngularJs NG重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这样的事情:

<div ng-controller="TestCtrl">
    <div ng-repeat="(k,v) in items | filter:hasSecurityId">
        {{k}} {{v.pos}}
    </div>
</div>

AngularJs部分:

AngularJs Part:

function TestCtrl($scope) 
{
    $scope.items = {
                     'A2F0C7':{'secId':'12345', 'pos':'a20'},
                     'C8B3D1':{'pos':'b10'}
                   };

    $scope.hasSecurityId = function(k,v)
    {
       return v.hasOwnProperty('secId');
    }
}

但不知何故,它显示我的所有项目。我如何可以筛选(键,值)?

But somehow, it is showing me all items. How can I filter on (key,value) ?

推荐答案

只能是应用到阵列和不是对象,从棱角分明的API -

Angular filters can only be applied to arrays and not objects, from angular's API -

选择从数组项的一个子集,并将其作为新的数组。

"Selects a subset of items from array and returns it as a new array."

您有两个选项:结果
1)移动 $ scope.items 到一个数组或 - 结果
2)pre-过滤 NG-重复的项目,如:

You have two options here:
1) move $scope.items to an array or -
2) pre-filter the ng-repeat items, like this:

<div ng-repeat="(k,v) in filterSecId(items)">
    {{k}} {{v.pos}}
</div>

和控制器上的:

$scope.filterSecId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('secId')) {
            result[key] = value;
        }
    });
    return result;
}

的jsfiddle http://jsfiddle.net/bmleite/WA2BE/

这篇关于如何筛选(键,值)在AngularJs NG重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆