$scope.toCart = function(id,item){
var toSaveArray = [];
var toSaveObj = {
id: item._id,
name : item.name,
image : item.image[0],
price : item.price,
discount_price : item.discount_price,
qty : 1
}
toSaveArray.push(toSaveObj);
var fromSaved = JSON.parse(localStorage.getItem('cart'));
if(fromSaved){
for(i=0;i<fromSaved.length;i++){
if(fromSaved[i].id == id){
fromSaved[i].qty += 1;
toSaveArray.push(fromSaved);
}
}
}
localStorage.setItem('cart',JSON.stringify(toSaveArray));
}
如果項目存在,添加一個qty,否則只是將該對象添加到數組中。但是用這個簡單的邏輯,我沒有一個工作代碼。以上代碼已損壞。努力與添加到購物車本地存儲邏輯
'上面的代碼broken'怎麼會這樣?它在做什麼? –
具體是什麼被打破? –
我認爲你的邏輯回到'toSaveArray.push(fromSaved);'從我所看到的,將'toSaveObj'推到現有的save('fromSaved')將會更有意義,然後重新保存。 – DBS