我有一个将ID映射到其对象的对象。我想显示这些对象的列表并使用过滤器,但我无法让它工作。具体来说,我试图阻止ID为2的对象出现在选项中这就是我所拥有的: http://jsfiddle.net/9d2Za/
<div ng-app ng-controller="ctrl">
Unfiltered:
<select ng-model="selectedBidType"
ng-options="bidType.id as bidType.label for (bidTypeID, bidType) in bidTypes">
</select>
<br>
Filtered:
<select ng-model="selectedBidType"
ng-options="bidType.id as bidType.label for (bidTypeID, bidType) in bidTypes | filter:{id: '!2'}">
</select>
</div>
请注意:我无法在我们提出的任何修复中更改bidTypes
对象的结构。这是我的AngularJS:
function ctrl($scope)
{
$scope.selectedBidType = 1;
// Bid type objects are indexed by ID for fast lookup (this cannot be changed in solution)
$scope.bidTypes = {
1: {id: 1, label: "Buy"},
2: {id: 2, label: "Sell"},
3: {id: 3, label: "Missing"}
};
}
答案 0 :(得分:2)
如documentation所述,filter
过滤器仅接受数组作为第一个参数,而不是对象。在这种情况下,我个人使用自定义过滤器进行转换:
myModule.filter(
'objectToArray',
[
function ()
{
return function (object)
{
var array = [];
angular.forEach(object, function (element)
{
array.push(element);
});
return array;
};
}
]
)
;
然后,在模板中:
<select ng-options="… in bidTypes | objectToArray | filter:{id:'!2'}">