I've got some code that does the following.... The first get call basically just gives a list of posts in data. Then for each post, there are a number of replies which I would like to load given by the second get call. The problem is, it actually goes through the loop but then only loads the last reply.
http.get("../api/sortpost/trending").success(function (data){
scope.posts = data;
for(var i = 0; i < scope.posts.length; i++){
post=scope.posts[i];
console.log(i); //prints 1 to i as expected
http.get("/api/postreply/"+post.id).success(function (data){
post.replies = data;
console.log(i); //prints i only i number of times
});
}
});
Anyone know how to fix? I am guessing that I am looping too quickly through and that the get request does not have enough time to process. Thus something funny is happening with my post.replies and i and the post is changing before the request is actually complete.