1

I am working on an app that is supposed to filter and sort out data from two json files. The app will have two tables that compare and contrast this data using ngRepeat myData. So far, the top table is already requesting a json file:

app.controller('tableTopController', function ($scope, $http) {
$http.get('first.json').success(function(response){
    $scope.myData = response; });

The bottom table is supposed to read data from my second json file: second.json.

2 Answers 2

7

Try using $q.all() to resolve both promises and execute a callback function when both are successful. For more info, see the docs.

var promises = [];

promises.push(getFirstJson());
promises.push(getSecondJson());

$q.all(promises).then(function (results) {
    var firstJson = results[0];
    var secondJson = results[1];
});

function getFirstJson() {
    return $http.get(...);
}

function getSecondJson() {
    return $http.get(...);
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to wait for the first call to complete before you get the second file and if you want to ensure everything is loaded before comparing and contrasting:

app.controller('theController', function ($scope, $http) {
    $http.get('first.json').success(function(response){
        $scope.firstData = response; 
        $http.get('second.json').success(function(response1){
            $scope.secondData = response1;
            //add any other logic you need to do here to compare & contrast
            //or add functions to $scope and call those functions from gui
        });
    });
});

Or, call them sequentially but then you need to ensure your comparing and contrasting can't start until both are loaded:

app.controller('theController', function ($scope, $http) {
    $http.get('first.json').success(function(response){
        $scope.firstData = response; 
    });
    $http.get('second.json').success(function(response1){
        $scope.secondData = response1;
    });
    //add any other logic you need in functions here to compare & contrast
    //and add those functions to $scope and call those functions from gui
    //only enabling once both firstData and secondData have content
});

4 Comments

But what if I need to call 9 json files?
@Anton that's not what the question asked and the solution you are looking for will depend on the detail of the way you need to call that data but the technique in my answer and the one in Lucas Ribeiro's answer may guide you towards the answer you are looking for; but, if not, and you can't find any other help on SO to guide you to your solution, why don't you ask another question with more detail about what you are trying to achieve?
I just don't want to duplicate same question, this question is pretty close to my, and your answer is perfectly work, I'm thinking how to decrease code, and thought may be exists some method
@Anton it seems like you want to post to Code Review

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.