微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算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>

谢谢。

模板中
<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;
}

原文地址:https://www.jb51.cc/angularjs/147015.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐