亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算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 關注
  • 886 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號