我正在尝试做类似的事情:
<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');
}
}
但不知何故,它向我展示了所有物品。如何过滤(键,值)?
答案 0 :(得分:126)
Angular filters只能应用于数组,而不能应用于角度的API -
&#34;从数组中选择项目的子集并将其作为新数组返回。&#34;
这里有两个选择:
1)将$scope.items
移动到数组或 -
2)预过滤ng-repeat
项,如下所示:
<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/
答案 1 :(得分:45)
我的解决方案是创建自定义过滤器并使用它:
app.filter('with', function() {
return function(items, field) {
var result = {};
angular.forEach(items, function(value, key) {
if (!value.hasOwnProperty(field)) {
result[key] = value;
}
});
return result;
};
});
在html中:
<div ng-repeat="(k,v) in items | with:'secId'">
{{k}} {{v.pos}}
</div>
答案 2 :(得分:25)
此外,您可以将foreach ($dbHandle->query('SELECT * FROM trainscheckedin', PDO::FETCH_ASSOC) as $row) {
$data[] = $row; // create an array of rows
}
echo json_encode($data);
// or possibly an object
echo json_encode((object)$data);
与ng-repeat
:
ng-if
答案 3 :(得分:21)
答案 4 :(得分:11)
您可以简单地使用angular.filter模块,然后甚至可以通过嵌套属性进行过滤
见:jsbin
2例:
<强> JS:强>
angular.module('app', ['angular.filter'])
.controller('MainCtrl', function($scope) {
//your example data
$scope.items = {
'A2F0C7':{ secId:'12345', pos:'a20' },
'C8B3D1':{ pos:'b10' }
};
//more advantage example
$scope.nestedItems = {
'A2F0C7':{
details: { secId:'12345', pos:'a20' }
},
'C8B3D1':{
details: { pos:'a20' }
},
'F5B3R1': { secId:'12345', pos:'a20' }
};
});
<强> HTML:强>
<b>Example1:</b>
<p ng-repeat="item in items | toArray: true | pick: 'secId'">
{{ item.$key }}, {{ item }}
</p>
<b>Example2:</b>
<p ng-repeat="item in nestedItems | toArray: true | pick: 'secId || details.secId' ">
{{ item.$key }}, {{ item }}
</p>
答案 5 :(得分:7)
有点晚了,但是我找了类似的过滤器,结束了这样的事情:
<div ng-controller="TestCtrl">
<div ng-repeat="(k,v) in items | filter:{secId: '!!'}">
{{k}} {{v.pos}}
</div>
</div>
答案 6 :(得分:0)
我做了一些我已经在多个项目中使用过的通用过滤器:
<强> HTML:强>
<input ng-model="customerNameFilter" />
<div ng-repeat="(key, value) in filter(customers, 'customerName', customerNameFilter" >
<p>Number: {{value.customerNo}}</p>
<p>Name: {{value.customerName}}</p>
</div>
<强> JS:强>
$scope.filter = function(object, field, filter) {
if (!object) return {};
if (!filter) return object;
var filteredObject = {};
Object.keys(object).forEach(function(key) {
if (object[key][field] === filter) {
filteredObject[key] = object[key];
}
});
return filteredObject;
};
答案 7 :(得分:0)
虽然这个问题相当陈旧,但我想分享我对角度1开发人员的解决方案。重点是重用原始的角度过滤器,但透明地将任何对象作为数组传递。
app.filter('objectFilter', function ($filter) {
return function (items, searchToken) {
// use the original input
var subject = items;
if (typeof(items) == 'object' && !Array.isArray(items)) {
// or use a wrapper array, if we have an object
subject = [];
for (var i in items) {
subject.push(items[i]);
}
}
// finally, apply the original angular filter
return $filter('filter')(subject, searchToken);
}
});
像这样使用它:
<div>
<input ng-model="search" />
</div>
<div ng-repeat="item in test | objectFilter : search">
{{item | json}}
</div>
这是一个plunker