admin管理员组

文章数量:1399269

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

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

Share Improve this question edited Aug 26, 2015 at 18:05 otterblox asked Aug 26, 2015 at 17:34 otterbloxotterblox 291 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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(...);
}

If you want to wait for the first call to plete before you get the second file and if you want to ensure everything is loaded before paring 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 pare & contrast
            //or add functions to $scope and call those functions from gui
        });
    });
});

Or, call them sequentially but then you need to ensure your paring 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 pare & contrast
    //and add those functions to $scope and call those functions from gui
    //only enabling once both firstData and secondData have content
});

本文标签: javascriptAngularJs How to call multiple json files to tables using httpgetStack Overflow