2013-03-17 53 views
1

的异步加载可以说我有此方法从服务填充的角度视图:Angularjs - 元素

$http.get("" + window.ENDPOINT + "/main_things/" + $scope.mainThingId + ".json").success(function(data) { 
    return $scope.main_thing = data; 
}); 

比方说,我想要的信息,另一位加载到我的网页,但其他位利用有些慢对外服务,所以我希望异步加载:

$http.get("" + window.ENDPOINT + "/secondary_things/" + $scope.parentMainThingId + ".json").success(function(data) { 
    return $scope.secondary_thing = data; 
}); 

我怎样组织我的代码在相位控制器,所以我可以先加载main_thing,然后加载次要的东西?

任何帮助,将不胜感激。

回答

3

通过运行在第一个请求的成功回调第二个请求:

$http.get("" + window.ENDPOINT + "/main_things/" + $scope.mainThingId + ".json") 
    .success(function(data) { 
    $scope.main_thing = data; 
    $http.get("" + window.ENDPOINT + "/secondary_things/" + $scope.parentMainThingId + ".json") 
     .success(function(data) { 
     $scope.secondary_thing = data; 
    }); 
    });