2

I'm fairly new to AngularJS so apologies in advance.

var tcores = 0;

angular.module('core').controller('HomeController', ['$scope', '$http',
  function($scope, $http) {
    $http.get('search?idc=LH5&type=Virtual&cluster=1').success(function(data) {
      $scope.servers = data; // get data from json
      angular.forEach($scope.servers, function(item){
        //console.log(item.cores);
        if(parseInt(item.cores) != "NaN" && item.cores != "") {
          if(angular.isNumber(parseInt(item.cores))) {
            tcores = parseInt(tcores) + parseInt(item.cores);
          }
        }
      })
      console.log("Hall 5. Cluster 1 Total Cores: " + tcores);
    });
  }
]);

angular.module('core').controller('HomeController', ['$scope', '$http',
  function($scope, $http) {
    $http.get('search?idc=LH5&type=Virtual&cluster=2').success(function(data2) {
      $scope.serverscluster = data2; // get data from json
      angular.forEach($scope.serverscluster, function(item){
        //console.log(item.cores);
        if(parseInt(item.cores) != "NaN" && item.cores != "") {
          if(angular.isNumber(parseInt(item.cores))) {
            tcores = parseInt(tcores) + parseInt(item.cores);
          }
        }
      })
      console.log("Hall 5. Cluster 2 Total Cores: " + tcores);
    });
  }
]);

I'm trying to run both functions (both are pointed at different JSON outputs), however I am only receiving a response for "cluster 2".

Please advise how I can run both functions in the same module, ideally I will be running 8 of these requests on the same page so advice is most appreciated.#

Can you also tell my why only the bottom function is executing?

Thank you very much!

3
  • 3
    You can't have two controllers with the same name you need to chain your $http.get in a unique controller name. Commented Aug 24, 2015 at 13:15
  • Thanks @Michelem - working now :) Please post as answer so I can give you the rep you deserve Commented Aug 24, 2015 at 13:19
  • 1
    I posted it with a working JSFiddle included. Thanks. Commented Aug 24, 2015 at 13:25

2 Answers 2

3

As I wrote on a comment you have to chain the $http.get together and you must avoid duplicate controller names. Here is a simplified code:

angular.module('core').controller('HomeController', ['$scope', '$http',

function ($scope, $http) {
    $http.get('search?idc=LH5&type=Virtual&cluster=1').then(function (data) {
        console.log(data);
        return $http.get('search?idc=LH5&type=Virtual&cluster=2');
    }).then(function(data2) {
        console.log(data2);
        $scope.serverscluster = data2; // get data from json
    });
}]);

And here is a JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you want to call your functions in single controller:

angular.module('core').controller('HomeController', ['$scope', '$http',
    function($scope, $http) {
        var success = function(data) {
            $scope.servers = data; // get data from json
            angular.forEach($scope.servers, function(item){
                //console.log(item.cores);
                if(parseInt(item.cores) != "NaN" && item.cores != "") {
                    if(angular.isNumber(parseInt(item.cores))) {
                        tcores = parseInt(tcores) + parseInt(item.cores);
                    }
                }
            });
            console.log("Hall 5. Cluster 1 Total Cores: " + tcores);
        };

        var error = function(data) {
            console.log('Something went terrible wrong', data);
        };


        $http.get('search?idc=LH5&type=Virtual&cluster=1')
            .then(success, error);

        $http.get('search?idc=LH5&type=Virtual&cluster=2')
            .then(success, error);
    }
]);

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.