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!

2 Answers 2

1

you are calculating total as:

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

and it should probably be:

total += (product.done / product.total);
Sign up to request clarification or add additional context in comments.

Comments

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;

1 Comment

Thanks for the input. I had problem searching the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.