【发布时间】:2015-02-10 22:09:08
【问题描述】:
我正在尝试创建一个 Ionic 应用程序,为此我必须在 for 循环中发出一些 HTTP GET 请求,但似乎 angular 不会等待显示之前的数据。
这是我正在使用的代码。
$http.get(ApiUrl.get() + '/Threads' + '?access_token=' + $scope.userToken + filterString + "&filter[order]=created%20desc")
.success(function(data, status, headers, config) {
var i=0;
for(thread in data)
{
$scope.threadObj = data[i];
var threadId = $scope.threadObj.id;
$scope.threadPostNumber;
//On récupére chaque nombre de post
$http.get(ApiUrl.get() + '/Threads/' + threadId + '/posts/count' + '?access_token=' + $scope.userToken)
.success(function(data, status, headers, config) {
$scope.threadPostNumber = data.count;
})
.error(function(data, status, headers, config) {
alert("Connection error, please try again.");
$location.path("/app/carte");
});
$scope.threadObj.count = $scope.threadPostNumber;
$scope.threads[i] = $scope.threadObj;
i++;
}
})
.error(function(data, status, headers, config) {
alert("Connection error, please try again.");
$location.path("/app/carte");
});
第一个 HTTP 获取已完成,数据可以在 foreach 中显示,但是当我尝试使用第二个获取请求将附加数据添加到原始数据时,没有创建任何内容,或者有时只创建最后一个显示每个值。
【问题讨论】:
-
很遗憾,您不能像那样使用 $http。 stackoverflow.com/a/17366158/3980115
-
@Vlad274 有没有办法在没有本地存储的情况下做到这一点?
标签: angularjs rest ionic-framework ionic