2016-06-20 10 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 todalしたいと思っていますどのように多くの製品の合計が欲しいのですか、またはすべてのprioductsのコストの合計(製品価格*数量)が欲しいですか? – Naitik

+0

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

+0

とまったく同じ質問が製品固有のものでなければなりません。 ng-model = "quantity"をng-model = "product.quantity"に置き換えてください – John

答えて

0

をインクリメントして、とにかく私は合計を保存することができます使用は、このためのNG-のinit

<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

のように私はあなたの変数を知らないが、私は私たちにエラーMSGを表示してくださいいない場合は、

{{product.price * product.quantity}} 

を逃すと仮定します?

+0

NaN i私はそれをより良いと思う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

関連する問題