2013-03-17 47 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; 
    }); 
    });