我需要根据 angularjs 中的另一个集合来过滤对象集合。我确信我需要创建一个自定义过滤器,但没有找到类似的示例。所以我有一个“设备选项”列表(基本上它们是产品定制,例如汽车中的高级音响)。除非您已经选择了其他“设备选项”,否则某些“设备选项”不可用。
可用的 EquipmentOptions 为 $scope.product.equipmentOptions。
已选择的 EquipmentOptions 为 $scope.product.selectedOptions。
equipmentOptions 有一个属性 requiredParents,它是其他设备选项的 id 集合。如果 $scope.product.selectedOptions 中存在这些 requiredParents 中的任何一个,则应显示 EquipmentOption。
到目前为止,我已经尝试过以下方法,但没有成功:
<div ng-repeat="option in product.equipmentOptions | optionsFilter:product.selectedOptions">
选项过滤器.js
myApp.filter('optionsFilter', function () {
return function (selectedOptions) {
// I'm just trying to get the list of selected options as well as the current option here for filtering, how do I get the current option?
};
});
最佳答案
您的第一个参数是product.equipmentOptions
列表。第二个参数是product.selectedOptions
。像这样编写过滤器:
myApp.filter('optionsFilter', function() {
return function(equipmentOptions, selectedOptions) {
var filteredOptions = [];
angular.forEach(equipmentOptions, function(option) {
if(/*option meets criteria*/) {
filteredOptions.push(option);
}
});
return filteredOptions;
}
});
关于javascript - 使用 AngularJS 自定义过滤器根据另一个集合过滤一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23913828/