0

Hello I want to be able to calculate some numbers inside a controller that contains elements. This is what have tried (ionic APP too btw) :

.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;
    };
})

and the page has:

 <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>

On the output I only see "{{ calculateProductivity() }}" and not the result, but if I write tasksCollection.info it gets correctly displayed. thanks!

Joseph Ocasio
  • 989
  • 4
  • 10
  • 19

2 Answers2

1

you are calculating total as:

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

and it should probably be:

total += (product.done / product.total);
Nema
  • 397
  • 2
  • 12
0
PLease see this: http://stackoverflow.com/questions/20942878/angularjs-use-a-function-in-a-controller-to-return-data-from-a-service-to-be-use

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;