Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs:如何使用http.get调用多个json文件到表中_Javascript_Json_Angularjs_Http - Fatal编程技术网

Javascript AngularJs:如何使用http.get调用多个json文件到表中

Javascript AngularJs:如何使用http.get调用多个json文件到表中,javascript,json,angularjs,http,Javascript,Json,Angularjs,Http,我正在开发一个应用程序,它应该能够从两个json文件中过滤和排序数据。该应用程序将有两个表,使用ngRepeat myData比较和对比这些数据。到目前为止,上表已经在请求一个json文件: app.controller('tableTopController', function ($scope, $http) { $http.get('first.json').success(function(response){ $scope.myData = response; }); 底部的

我正在开发一个应用程序,它应该能够从两个json文件中过滤和排序数据。该应用程序将有两个表,使用ngRepeat myData比较和对比这些数据。到目前为止,上表已经在请求一个json文件:

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

底部的表应该从我的第二个json文件second.json中读取数据

如果要在获取第二个文件之前等待第一次调用完成,并且要确保在比较和对比之前加载所有内容:

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
        });
    });
});
或者,按顺序调用它们,但您需要确保在加载这两者之前无法开始比较和对比:

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
});

如果要在获取第二个文件之前等待第一次调用完成,并且要确保在比较和对比之前加载了所有内容,请执行以下操作:

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
        });
    });
});
或者,按顺序调用它们,但您需要确保在加载这两者之前无法开始比较和对比:

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
});
尝试使用
$q.all()
解析这两个承诺,并在两者都成功时执行回调函数。有关更多信息,请参阅

尝试使用
$q.all()
解析这两个承诺,并在两者都成功时执行回调函数。有关更多信息,请参阅


但是如果我需要调用9个json文件怎么办?@Anton这不是问题所在,您正在寻找的解决方案将取决于您调用该数据的方式的细节,但我的答案中的技巧以及Lucas Ribeiro的答案中的技巧可能会引导您找到您想要的答案;但是,如果没有,而且你找不到任何其他帮助来引导你找到解决方案,为什么你不提出另一个问题,更详细地说明你想要实现什么?我只是不想重复同一个问题,这个问题非常接近我的问题,你的答案非常有效,我正在考虑如何减少代码,思想可能是存在的method@Anton看起来你想发帖,但如果我需要调用9个json文件呢?@Anton这不是问题所在,你要寻找的解决方案将取决于你调用该数据的方式的细节,但我的答案和Lucas Ribeiro答案中的技巧可能会指导你走向你所寻找的答案;但是,如果没有,而且你找不到任何其他帮助来引导你找到解决方案,为什么你不提出另一个问题,更详细地说明你想要实现什么?我只是不想重复同一个问题,这个问题非常接近我的问题,你的答案非常有效,我正在考虑如何减少代码,思想可能是存在的method@Anton好像你想发邮件到