0

In the below code,

app.controller('Controller', function($scope, $http){

    $scope.rep = [];

    $scope.tot = {
        name: '',
        marketValue: 0,
        cash: 0,
        legend: 'none'
    };


    (function loadData(){
        $http.get('result.json').
            then(function(data) {
                        angular.forEach(data['data'],function(value, key){
                            $scope.rep[key] = value;
                            $scope.rep[key].marketValue    = parseFloat(value.marketValue);
                            $scope.rep[key].cash           = parseFloat(value.cash);
                        });
                    } ,
                     function(data) {
                        console.log("My error: " + data);
                    }
                );
    })(); // IIFE

    for (var i = 0; i < $scope.rep.length; i++) {
        $scope.tot.marketValue += $scope.rep[i].marketValue;
        $scope.tot.cash += $scope.rep[i].cash;
    }
});

$scope.rep is a local variable that is accessed in nested functions of IIFE.

After IIFE execution, no further execution happens, calm termination of execution.

Console does no give any error message.

Why for-loop does not execute?

2
  • Because AJAX stands for asynchronous... and you're hitting your loop before the $http call comes back. Commented Jan 6, 2016 at 21:32
  • http.get call is asynchronous, so when the loop executes, rep.length is still 0. Commented Jan 6, 2016 at 21:32

2 Answers 2

1

It's possible this could be a timing issue. Perhaps the for-loop is executing before the $http.get() returns a result. Have you tried executing the for-loop after $scope.rep is initialized like so?

(function loadData(){
    $http.get('result.json').
        then(function(data) {
                    angular.forEach(data['data'],function(value, key){
                        $scope.rep[key] = value;
                        $scope.rep[key].marketValue    = parseFloat(value.marketValue);
                        $scope.rep[key].cash           = parseFloat(value.cash);
                    });
                } ,
                 function(data) {
                    console.log("My error: " + data);
                }
            );
        })
        .then(function() {
          for (var i = 0; i < $scope.rep.length; i++) {
            $scope.totals.marketValue += $scope.rep[i].marketValue;
            $scope.totals.cash += $scope.rep[i].cash;
          }
        })(); // IIFE
Sign up to request clarification or add additional context in comments.

Comments

1

$http.get() returns a promise, and only once that promise gets resolved does it run all the code in the .then() blocks. (Either the first success callback one if the promise was resolved, or the second error callback if the promise is rejected.)

By the time your code reaches the for loop, you've received a promise object from the $http.get(), but you haven't yet run the code inside the .then() block, where you're setting values inside $scope.rep. The for loop doesn't execute because $scope.rep.length is 0, so no iterations.

To fix this, you could put your for loop inside your .then() block like this:

(function loadData() {
    $http.get('result.json')
        .then(function (data) {
            angular.forEach(data['data'], function (value, key) {
                $scope.rep[key] = value;
                $scope.rep[key].marketValue = parseFloat(value.marketValue);
                $scope.rep[key].cash = parseFloat(value.cash);
            });

            for (var i = 0; i < $scope.rep.length; i++) {
                $scope.tot.marketValue += $scope.rep[i].marketValue;
                $scope.tot.cash += $scope.rep[i].cash;
            }
        }, function (data) {
            console.log("My error: " + data);
        });
})();

I believe that should do it.

Comments

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.