1

可以说我有一个从服务中填充的角度视图:

$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 然后加载次要的东西?

任何帮助,将不胜感激。

4

1 回答 1

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;
    });
  });
于 2013-03-17T13:59:32.903 回答