0
添加訂單時,我想查找總價格的購物車。 這是我的代碼:如何在訂購商品時找到小計。 請幫我找到這個?使用angularjs在訂購商品中查找總數和小計總數?
mainCtrl.controller("OrderController",function($scope,function(){
var itemlist={}
$scope.subtotal=function(){
var amount = 0;
for(var i = 0; i < $scope.itemlist.length; i++){
angular.forEach($scope.itemlist[i].itemname, function(){
if($scope.itemlist[i].itemname==MenuItem.itemname){
var product = $scope.itemlist[i];
product.qty=product.qty+1;
amount += (product.itemprice * product.qty);
}
})
console.log("this is cart amount" + amount)
return amount;
}
}
}
$scope.add=function(item){
var MenuItem={"itemid":item.itemid,"itemname":item.itemname,"itemimage":item.itemimage,"qty":1,"itemprice":item.itemprice,"subtotal":subtotal(),"tax":0,"total":$scope.subtotal()}
var exists=false;
angular.forEach($scope.itemlist, function (item) {
if ((item.itemname == MenuItem.itemname)) {
exists=true;
item.qty++;
return false
}
})
if(!exists){
itemlist.push(MenuItem);
growl.success("Your Item added Sucessfully",{title:"Success"});
}
}
})
可能是你可以使用$看..如下 $範圍。$表( 'ITEMLIST',函數(NEWVALUE,屬性oldValue){// 迭代通過你的清單,計算總和並將其分配給範圍變量 }); }); $ watch監視項目是否已更改,並在更改時調用邏輯。 –
你能否提供一個帶有樣本輸入的蹲跳者工作演示? – Abhi
爲什麼你要在「$ scope.itemlist [i] .itemname」上做第二個循環,它是一個數組嗎? (名稱不明) – Aliz