2016-04-07 50 views
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"}); 
    } 
} 
}) 
+0

可能是你可以使用$看..如下 $範圍。$表( 'ITEMLIST',函數(NEWVALUE,屬性oldValue){// 迭代通過你的清單,計算總和並將其分配給範圍變量 }); }); $ watch監視項目是否已更改,並在更改時調用邏輯。 –

+0

你能否提供一個帶有樣本輸入的蹲跳者工作演示? – Abhi

+0

爲什麼你要在「$ scope.itemlist [i] .itemname」上做第二個循環,它是一個數組嗎? (名稱不明) – Aliz

回答

0
mainCtrl.controller("OrderController",function($scope,function(){ 
var itemlist={} 

$scope.add=function(item){ 
var MenuItem={"itemid":item.itemid,"itemname":item.itemname,"itemimage":item.itemimage,"qty":1,"itemprice":item.itemprice,"subtotal":"","tax":0,"total":""} 
var exists=false; 
angular.forEach($scope.itemlist, function (item) { 
    if ((item.itemname == MenuItem.itemname)) { 
     exists=true; 
     item.qty=item.qty+1; 
     item.subtotal=item.qty*item.itemprice; 
     item.total=item.subtotal 
     return false 
    } 
}) 
if(!exists){ 
    MenuItem.subtotal=MenuItem.qty*MenuItem,itemprice; 
    MenuItem.total=MenuItem.subtotal; 
    itemlist.push(MenuItem); 
    growl.success("Your Item added Sucessfully",{title:"Success"}); 
} 
} 
}) 

爲我工作