2014-10-20 63 views

回答

1

簡單的解決方案,讓您的當前結構大多保存完好迭代的JSON:

將視圖從{{total}}更改爲{{total()}}並執行您的功能,如下所示:

$scope.total = function() { 
    var total = 0; 
    angular.forEach($scope.items, function(item) { 
     total += item.price * item.qty; 
    }) 

    return total; 
} 

更新小提琴:http://jsfiddle.net/t4bbj8h2/

請記住,這遠非一個理想的解決方案,但沒有更多的信息你正在嘗試做在這裏我們不能給你太多的幫助提高你的應用程序。

0

您可以添加方便underscore.js庫,並做

$scope.total = function() { 
    var total = 0; 
    _.map($scope.items, function(item) { 
     total += parseFloat(item.price || 0) * parseFloat(item.qty || 0); 
    }); 
    return total; 
} 
0

找到了解決辦法:

http://jsfiddle.net/75m7e/745/

$scope.total = function() { 
     var total = 0; 
     angular.forEach($scope.items, function(item) { 
      if(item.isProduct=="1") { 
      total += item.price * item.qty; 
      } else if (item.isProduct=="0"){ 
      total += (item.weight/1000) * item.price ; 
      } 

     }) 

     return total; 
    } 
+0

不要忘記更換'{總}}'來'{{總()}}'。 – 2014-10-20 11:27:17