我有一个复杂的数据结构(购物车的产品),如下所示:
items = [
{
id_product: 5,
combinations: [
{
id_product_attribute: 35,
quantity: 1,
price: 25.60
},
{
id_product_attribute: 38,
quantity: 4,
price: 25.60
}
]
},
{
id_product: 5,
combinations: [
{
id_product_attribute: 35,
quantity: 1,
price: 28.60
}
]
}
];
我还有一个ng-repeat
,在li
个元素中列出了从$resource
检索到的产品的每个组合。每个li
元素都如下所示:
<li>Combination {{ combination.id }} - Price {{ combination.price }} - In your shopping cart {{ function_goes_here(product.id, combination.id) }}</li>
我想知道是否有可能编写某种功能,可以在购物车数据结构中搜索(按产品ID和组合ID)并返回数量元素,这样可以更新视图购物车中添加了相同类型的组合?
答案 0 :(得分:2)
在$ scope / scope上定义该函数:
$scope.items = [
{
id_product: 5,
combinations: [
{
id_product_attribute: 35,
quantity: 1,
price: 25.60
},
{
id_product_attribute: 38,
quantity: 4,
price: 25.60
}
]
},
{
id_product: 5,
combinations: [
{
id_product_attribute: 35,
quantity: 1,
price: 28.60
}
]
}
];
$scope.function_goes_here = function(productId, combinationId){
//search through $scope.items
return "Lorem Ipsum";
}