本文介绍了如何在 AngularJs 中使用 ng-repeat 过滤(键、值)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做类似的事情:
<div ng-repeat="(k,v) in items | filter:hasSecurityId">{{k}} {{v.pos}}
我正在尝试做类似的事情:
<div ng-repeat="(k,v) in items | filter:hasSecurityId">{{k}} {{v.pos}}
AngularJs 部分:
function TestCtrl($scope){$scope.items = {'A2F0C7':{'secId':'12345', 'pos':'a20'},'C8B3D1':{'pos':'b10'}};$scope.hasSecurityId = function(k,v){返回 v.hasOwnProperty('secId');}}
但不知何故,它向我展示了所有项目.如何过滤 (key,value) ?
Angular filters 只能是应用于数组而不是对象,来自 angular 的 API -
从数组中选择项目的子集并将其作为新数组返回."
这里有两个选择:
1) 将 $scope.items
移动到数组或 -
2) 预过滤 ng-repeat
项,如下所示:
{{k}} {{v.pos}}
在控制器上:
$scope.filterSecId = function(items) {变量结果 = {};angular.forEach(items, function(value, key) {如果 (!value.hasOwnProperty('secId')) {结果[键] = 值;}});返回结果;}
jsfiddle:http://jsfiddle.net/bmleite/WA2BE/
I am trying to do something like :
<div ng-controller="TestCtrl">
<div ng-repeat="(k,v) in items | filter:hasSecurityId">
{{k}} {{v.pos}}
</div>
</div>
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) ?
Angular filters can only be applied to arrays and not objects, from angular's API -
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>
And on the Controller:
$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-repeat 过滤(键、值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!