如何获得scorecard.run字段值的总和

时间:2015-07-06 13:33:46

标签: angularjs angularjs-ng-repeat

JS 我想创建返回 scorecard.runs总数的函数 我试过但它在浏览器中显示NAN值 这是我的代码

mainApp.controller("playerController", function($scope) {


$scope.player = 
   {
      firstName: "Rohit",
      lastName: "Sharma",
      country: "India",

      scorecard:
      [
         {team:'Australlia',run:1255},
         {team:'Srilanka',run:1195},
         {team:'Pakistan',run:2595},

      ],

      totalRuns: function() 
      {
        var total =  0;
        for (int i = 0 ; i < $scope.player.scorecard.length ; i++ )
        {
            total += $scope.player.scorecard[i].run; << here
        }
        return total ;
       }


   };
});

谢谢

2 个答案:

答案 0 :(得分:0)

尝试更改

  total += score.run[i];

  total += $scope.player.scorecard[i].run;

我不确定我是否遗漏了您从score.run获得的内容,但似乎根据您的逻辑,您正在寻找$scope.player.scorecard[i].run 。我假设这是因为你正在$scope.player.scorecard.length上循环。

另外 - 只是抬头 - 你在fullName函数之后缺少一个逗号

  fullName: function() 
  {
     var playerObject;
     playerObject = $scope.player;
     return playerObject.firstName + " " + playerObject.lastName;
  },    << here

答案 1 :(得分:-1)

我认为你需要增加像这样的总运行:

total += $scope.player.scorecard[i].run;