gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:03:43 25 4
gpt4 key购买 nike

我想放置这个过滤器:filter:'!Category' 像这样添加到这个元素上:

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

但它不会过滤掉属性“Category”。但是,如果我在以下 ng-repeat 元素上放置类似的过滤器来排除“Wine”,则效果很好:

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

我不明白为什么我不能在这里过滤掉属性值。我对 AngularJS 很陌生,我正在疯狂地试图解决这个问题。我想从对象中排除特定的属性名称。 “名称”或“类别”

我链接了下面的 fiddle 。非常感谢任何帮助!谢谢!

jsfiddle <- fiddle 链接

<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();
};
});

最佳答案

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

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/

希望这有帮助。

关于javascript - ng-repeat 过滤器从对象中否定字符串 - angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25322035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com