通过ng-repeat内的键过滤对象

时间:2014-11-04 00:02:02

标签: javascript angularjs

我想要排除未列为默认值但仍会显示每个项目的其他项目

<ul ng-controller="Ctrl" class="dropdown-menu">
    <li ng-repeat="(key, value) in Employee.KeyValue | filter:DefaultKeys(key) ">{{key}}</li>
</ul>

angular.module('app', []);

function Ctrl($scope) {
    $scope.DefaultKeys = function(item) {
        var defaultItems = ["SSS No.", "TIN"];

        return defaultItems.indexOf(item);
    };

    $scope.Employee =
        {
            Code: '123',            
            KeyValue:
            {
                'TIN': '9038468',
                'Facebook' : 'https://fb.com/abc'
            }
        }
}

选中此fiddle

请帮助

1 个答案:

答案 0 :(得分:1)

您可以制作自定义过滤器。

过滤

angular.module('app', []).filter('DefaultKeys', function () {
    return function (item) {
        var keys = {};
        angular.forEach(['SSS No.', 'TIN'], function (key) {
            if (item[key] !== undefined) {
                keys[key] = item[key];
            }
        });
        return keys;
    };
});

<强> HTML

<ul ng-controller="Ctrl" class="dropdown-menu">
    <li ng-repeat="(key,value) in Employee.KeyValue | DefaultKeys">{{key}}</li>
</ul>

DEMO