为了账号安全,请及时绑定邮箱和手机立即绑定

计算AngularJS ng-repeat中重复元素的总和

计算AngularJS ng-repeat中重复元素的总和

蝴蝶不菲 2019-07-30 11:25:39
计算AngularJS ng-repeat中重复元素的总和下面的脚本显示了使用的购物车ng-repeat。对于数组中的每个元素,它显示项目名称,其数量和小计(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条经验 获得超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;}


查看完整回答
反对 回复 2019-07-30
  • 3 回答
  • 0 关注
  • 831 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信