我有以下代码:
$scope.showTotal = function() {
$scope.pT = [];
var iT = 0;
for (var i = 0; i < $scope.od.length; i++) {
console.log($scope.od[i]['bpr']);
iT += $scope.od[i]['bpr'];
// also tried this -> iT = iT + $scope.od[i]['bpr']
}
$scope.pT.push({iTotal: iT});
console.log($scope.popupTotals);
$scope.showPopupNow = true;
}
但我不知道为什么它不起作用。
如果bpr是例如50和43.1034,那么它会在控制台中记录输出,如iTotal:"050.000043.1034"
我是JavaScript的新手,我直接使用AngularJS启动它。 所以请帮我解决JS中的算术运算符。 谢谢。
答案 0 :(得分:2)
Alt+3
答案 1 :(得分:1)
你正在循环中增加i。删除副本i,我怀疑你的$scope.order[i]['baseprice']
不是整数。因此,使用parseFloat
$scope.showTotal = function(){
$scope.popupTotals = [];
var itemtotal = 0;
for (var i = 0; i<$scope.order.length; i++){
console.log($scope.order[i]['baseprice']);
itemtotal += parseFloat($scope.order[i]['baseprice']);
//also tried this -> itemtotal = itemtotal + $scope.order[i]['baseprice']
//i++; No need to increment here
}
$scope.popupTotals.push({itembasetotal : itemtotal});
console.log($scope.popupTotals);
$scope.showPopupNow = true;
}