我为get total创建了代码。但它不会增加价值。所有值都将显示为一行。这段代码有什么问题。
ProductController.js
$scope.getTotal = function(type) {
var total = 0;
angular.forEach($scope.products, function(el) {
total += el[type];
});
return total;
};
cart.html
<table class="cartdata">
<tr>
<th class="a">Name</th>
<th class="a">Price</th>
<th class="a">Post Date</th>
<th class="a">Remove Item</th>
</tr>
<tr ng-repeat="produ in products track by $index" ng-init="setTotals(produ)">
<td class='b'>{{produ.post_title}}</td>
<td class="b">{{produ.ID | currency}}</td>
<td class="b">
<a ng-click="delete(post.id, $index)" class="btn btn-danger"></a>
</td>
</tr>
<td>Total{{getTotal('ID') | currency }}</td>
</table>
购物车视图
-------------------------
| Name | Price |
|------------------------
|Product 1 | $20.00 |
|Product 2 | $35.00 |
|Product 3 | $10.00 |
|-----------------------|
Total = $ 203,510.00
答案 0 :(得分:3)
检查一下 - fiddle
$scope.getTotal = function(type) {
var total = 0;
angular.forEach($scope.products, function(el) {
total += parseFloat(el[type]);
});
return total;
};