我想通过条形码过滤我的库存,我可以这样做吗?这里的要点是,如果我传递的条形码等于库存[i]条形码(在服务器中),则将$scope.inventories
更改为仅与条形码匹配的条形码。然后$ scope.inventories只包含匹配的条形码库存。
有些疯狂的错误。任何帮助,如何过滤对象数据?
$scope.inventories = new Inventory().query().$object;
$scope.filter = function(barcode) {
for(var i = 0; i < $scope.inventories.length; i++){
if($scope.inventories[i].barcode == barcode){
$scope.inventories = $scope.inventories[i];
}
}
};
模板
<ul ng-repeat="inventory in inventories">
<li><a href="" ng-click="filter(inventory.barcode)">{{inventory.barcode}}</a></li>
</ul>
答案 0 :(得分:0)
为过滤的商品创建不同的变量名称
$scope.filter = function(barcode) {
var filtered = [];
for(var i = 0; i < $scope.inventories.length; i++){
if($scope.inventories[i].barcode == barcode){
filtered.push($scope.inventories[i]);
}
}
$scope.filtered_inventories = filtered;
};
答案 1 :(得分:0)
定义过滤器:
$scope.filterByBarcode = function(list, barcode) {
return list.filter(function(item){
return item.barcode === barcode;
});
};
在ng-repeat:
列表中过滤<ul ng-repeat="inventory in filterByBarcode(inventories, barcode)">
<li><a href="" ng-click="inventory">{{inventory.barcode}}</a></li> <!-- Not sure if this is what you want to display -->
</ul>