ng-repeat过滤器否定对象中的字符串 - angularjs

时间:2014-08-15 06:18:44

标签: javascript angularjs angularjs-ng-repeat

我想把这个过滤器:过滤器:'!类别'    像这样的元素:

<div ng-repeat="(prop, ignoredValue) in wines[0] | filter:'!Category'" ng-init="filter[prop]={}">

但它没有过滤掉属性&#34;类别&#34;。但是,如果我使用类似的过滤器排除&#34; Wine&#34;在下面的ng-repeat元素中,它工作正常:

<span class="quarter" ng-repeat="opt in getOptionsFor(prop) | filter:'!Wine'">

我不明白为什么我可以在这里过滤掉属性值。我对angularjs很新,我想出去试图解决这个问题。我想从对象中排除特定的属性名称。要么&#34;名字&#34;或&#34;类别&#34;

我把下面的小提琴联系起来。任何帮助非常感谢!谢谢!

jsfiddle&lt; - 链接到小提琴

<div ng-controller="myCtrl">
<div ng-repeat="(prop, ignoredValue) in wines[0]" ng-init="filter[prop]={}">
    <b>{{prop | capitalizeFirst}}:</b><br />
    <span class="quarter" ng-repeat="opt in getOptionsFor(prop)">
        <b><input type="checkbox" ng-model="filter[prop][opt]" />&nbsp;{{opt}}</b>
    </span>
    <hr />
</div>
<div ng-repeat="w in filtered=(wines | filter:filterByProperties)">
    {{w.name}} ({{w.category}})
</div>
<hr />
Number of results: {{filtered.length}}

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.wines = [
    { name: "Wine A", category: "red" },
    { name: "Wine B", category: "red" },
    { name: "wine C", category: "white" },
    { name: "Wine D", category: "red" },
    { name: "Wine E", category: "red" },
    { name: "wine F", category: "white" },
    { name: "wine G", category: "champagne"},
    { name: "wine H", category: "champagne" }    
];
$scope.filter = {};

$scope.getOptionsFor = function (propName) {
    return ($scope.wines || []).map(function (w) {
        return w[propName];
    }).filter(function (w, idx, arr) {
        return arr.indexOf(w) === idx;
    });
};

$scope.filterByProperties = function (wine) {
    // Use this snippet for matching with AND
    var matchesAND = true;
    for (var prop in $scope.filter) {
        if (noSubFilter($scope.filter[prop])) continue;
        if (!$scope.filter[prop][wine[prop]]) {
            matchesAND = false;
            break;
        }
    }
    return matchesAND;
/**/
/*
    // Use this snippet for matching with OR
    var matchesOR = true;
    for (var prop in $scope.filter) {
        if (noSubFilter($scope.filter[prop])) continue;
        if (!$scope.filter[prop][wine[prop]]) {
            matchesOR = false;
        } else {
            matchesOR = true;
            break;
        }
    }
    return matchesOR;
/**/
};

function noSubFilter(subFilterObj) {
    for (var key in subFilterObj) {
        if (subFilterObj[key]) return false;
    }
    return true;
}
});

app.filter('capitalizeFirst', function () {
return function (str) {
    str = str || '';
    return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
};
});

1 个答案:

答案 0 :(得分:1)

您可以编写一个简单的过滤器来首先提取密钥:

app.filter('keys', function () {
    return function (object) {
        return Object.keys(object || {}).filter(function (key) {
            return key !== '$$hashKey'; // this is from ng-repeat
        });
    };
});

并像这样使用它:

<div ng-repeat="prop in wines[0] | keys | filter:'!Category'"

示例JSFiddle: http://jsfiddle.net/1qhba9fs/1/

希望这有帮助。