AngularJs- 控制器内部的计算

AngularJs- Calculation inside a controller

您好,我希望能够在包含元素的控制器中计算一些数字。这是已经尝试过的方法(离子 APP 也顺便说一句):

.controller('tasksCtrl', function($scope) {
  $scope.tasksCollection = [
    { info: 'Go studying', measured: 'no', total: 1, done: 1, id: 1 },
    { info: 'Go to the beach', measured: 'no', total: 1, done: 1, id: 2},
    { info: 'Run', measured: 'yes', total: 30, done: 15, id: 3}
    ];

    $scope.calculateProductivity = function(){
    var total = 0;
    for(var i = 0; i < $scope.tasksCollection.length; i++){
        var product = $scope.tasksCollection[i];
        total += (tasksCollection.done / tasksCollection.total);
      }
      return total;
    };
})

并且页面有:

 <div class="item tabs tabs-secondary tabs-icon-left">
          <div  ng-app="starter" ng-controller="tasksCtrl" class="tab-item tab-main-left">
            <span class="title-red"> {{ calculateProductivity() }} </span><span class="medium-text">Productivity Ratio</span>
          </div>
          <div class="tab-item tab-main-right">
            <span class="title-red">20 </span><span class="medium-text">PMoney</span>
          </div>

        </div>

在输出中我只看到“{{ calculateProductivity() }}”而不是结果,但是如果我写 tasksCollection.info 它会正确显示。 谢谢!

您计算的总数为:

total += (tasksCollection.done / tasksCollection.total);

大概应该是:

total += (product.done / product.total);
PLease see this: 

The following changes should work:

<span class="title-red"> {{ productivity_ratio }} </span><span class="medium-text">Productivity Ratio</span>

for(var i = 0; i < $scope.tasksCollection.length; i++){
        var product = $scope.tasksCollection[i];
        total += (tasksCollection.done / tasksCollection.total);
   }
$scope.productivity_ratio = total;