2016-06-20 39 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