2016-06-20 80 views
1

我試圖讓ngRepeat的我這裏有之,我想這樣做的HTML文件有反正我可以存儲總和爲它增加總結在angularjs

<div class = "row" ng-repeat= "product in products"> 
    <div class = "col"><input type="checkbox" name="check" value="{{product.product_id}}"></div> 
    <div class = "col">{{product.product_name}}</div> 
    <div class = "col"><input type="text" ng-model="quantity"></div> 
    <div class = "col">{{product.price * quantity}}</div> 
</div> 

    <div class = "row" align="center"> 
     <div class="col"> 
     <strong>Total : {{total}}</strong> 
     </div> 
+0

你想總結什麼?你可以編輯你的問題,並給予更多的清晰度?你想要ngRepeast的意思是你想要多少產品的總數,或者你想要所有產品的總成本{產品價格*數量}? – Naitik

+0

與http://stackoverflow.com/questions/22731145/calculating-sum-of-repeated-elements-in-angularjs-ng-repeat(與答案) – Keugels

+0

數量完全相同的問題應該是產品特定的。用ng-model =「product.quantity」替換ng-model =「quantity」 – John

回答

0

你可以採用NG-初始化這個像

<div ng-init="items.total = {}"> 
    <div class = "row" ng-repeat= "product in products" > 
      <div class = "col"><input type="checkbox" name="check" value="{{product.product_id}}"></div> 
      <div class = "col">{{product.product_name}}</div> 
      <div class = "col"><input type="text" ng-model="quantity"></div> 
      <div class = "col" ng-init="items.total.quantity= items.total.quantity + product.price * quantity">{{product.price * quantity}}</div> 
     </div> 

      <div class = "row" align="center"> 
       <div class="col"> 
       <strong>Total : {{items.total.quantity}}</strong> 
       </div> 
      </div> 

    </div> 
+0

也許init應該在ng-repeat之外 – ThomasP1988

+0

我可以在我的app.js中這樣做,所以我可以計算所有總量 – olajide

+0

是,計算應該是在JS ...但爲此,你必須在js –

0

我不知道你的變量,但我相信你錯過

{{product.price * product.quantity}} 

如果不是請告訴我們錯誤味精?

+0

它顯示NaN我認爲它更好我做它app.js所以我可以將其轉換爲數字 – olajide

0

最好不要在html中擁有你的邏輯。但如果你想你可以試試這個

<div ng-init="temp={total:0}"> 
    <div class = "row" ng-repeat= "product in products" ng-init="total=0"> 
     <div class = "col" ng-init="temp.total=temp.total+product.price"><input type="checkbox" name="check" value="{{product.product_id}}"></div> 
     <div class = "col">{{product.product_name}}</div> 
     <div class = "col"><input type="text" ng-model="quantity"></div> 
     <div class = "col">{{product.price * quantity}}</div> 
    </div> 
</div> 

     <div class = "row" align="center"> 
      <div class="col"> 
      <strong>Total : {{temp.total}}</strong> 
      </div> 
+0

謝謝這實際上幫助 – olajide

+0

我想我會按照你的意見,我會做到這一點在app.js – olajide