計算AngularJS ng-repeat中重復元素的總和下面的腳本顯示了使用的購物車ng-repeat。對于數(shù)組中的每個元素,它顯示項目名稱,其數(shù)量和小計(product.price * product.quantity)。計算重復元素總價的最簡單方法是什么?<table>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr ng-repeat="product in cart.products">
<td>{{product.name}}</td>
<td>{{product.quantity}}</td>
<td>{{product.price * product.quantity}} €</td>
</tr>
<tr>
<td></td>
<td>Total :</td>
<td></td> <!-- Here is the total value of my cart -->
</tr></table>
3 回答

神不在的星期二
TA貢獻1963條經(jīng)驗 獲得超6個贊
在模板中
<td>Total: {{ getTotal() }}</td>
在控制器中
$scope.getTotal = function(){ var total = 0; for(var i = 0; i < $scope.cart.products.length; i++){ var product = $scope.cart.products[i]; total += (product.price * product.quantity); } return total;}
- 3 回答
- 0 關注
- 884 瀏覽
添加回答
舉報
0/150
提交
取消