首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJs:如何使用http.get对表调用多个json文件

AngularJs:如何使用http.get对表调用多个json文件
EN

Stack Overflow用户
提问于 2015-08-26 17:34:08
回答 2查看 5.2K关注 0票数 1

我正在开发一个应用程序,用来过滤和整理两个json文件中的数据。该应用程序将有两个表,使用ngRepeat myData对这些数据进行比较和对比。到目前为止,顶层表已经在请求一个json文件:

代码语言:javascript
复制
app.controller('tableTopController', function ($scope, $http) {
$http.get('first.json').success(function(response){
    $scope.myData = response; });

下面的表应该读取我的第二个json文件: second.json中的数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-26 17:43:05

如果您希望在获得第二个文件之前等待第一个调用完成,并且希望在进行比较和对比之前确保加载了所有内容:

代码语言:javascript
复制
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
        });
    });
});

或者,按顺序调用它们,但是您需要确保在加载两种方法之前,您的比较和对比都不能启动:

代码语言:javascript
复制
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
});
票数 1
EN

Stack Overflow用户

发布于 2015-08-26 17:51:42

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

代码语言:javascript
复制
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(...);
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32233087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档