【问题标题】:summing up number of angular.foreach loop总结 angular.foreach 循环的数量
【发布时间】:2017-04-24 01:51:15
【问题描述】:

我试图在一个简单的函数中总结几个数字。我遍历我的数字列表,可以从日志中看到我看到的每个数字。

然后我有以下内容,其中有一个 var 保存总和并应返回它。

但是什么也没发生!

代码如下:

  angular.extend(self, {

    getTotalGood : function(){
      var total = 1;
      angular.forEach( self.myproducts, function( value, key ){
        console.log(key + ' : ' + value.product.cost);
        total = value.product.cost;
      })
      return total;
    }

  })

  self.getTotalGood();

【问题讨论】:

    标签: angularjs angular-controller


    【解决方案1】:

    改变你的方法如下:

     getTotalGood : function(){
      var total = 0;
      angular.forEach( self.myproducts, function( value, key ){
        console.log(key + ' : ' + value.product.cost);
        total += value.product.cost;
      })
      console.log("total: " + total);
      return total;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多