Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an $http request going out and fetching some JSON that is being returned in a structure like this:

{
  "news": [
    {
      "item1": "information1",
      "item2": "information2",
      "item3": "information3",
      "itemId": "ID1"
    },
    {
      "item1": "information1",
      "item2": "information2",
      "item3": "information3",
      "itemId": "ID2"
    },
    {
      "item1": "information1",
      "item2": "information2",
      "item3": "information3",
      "itemId": "ID3"
    },
  ]
}

Now, I can use ng-repeat to get to the news items just fine, but I need to make another $http request with the ID that is nested in each news item. Basically, I need to use that ID to find a name so that my news items are no longer just bits of information with an ugly ID, but bits of information with a user-friendly name. I already have the code returning, what I think, is the ID for each of those news items:

$http.jsonp('http://website/api/news&jsonp=JSON_CALLBACK')
    .success(function (result) {
        $scope.news = result.news;

        $scope.items = [];
        angular.forEach(result.news, function (item, index) {
            $scope.items.push(item);

            $http.jsonp('http://website/api/' + $scope.items.itemId + '?jsonp=JSON_CALLBACK')
                .success(function (result) {
                    $scope.fullItem = result.data;
                })
                .error(function () {
                    $log.warn("Something broke.");
                });
        });
    })
    .error(function () {
        $log.warn("Something broke.");
    });

As always, any help you all could provide would be very much appreciated.

share|improve this question
    
What actually you want to achieve? You can have $http call inside loop for each IDs. – Jay Shukla May 3 '14 at 6:16
    
Sounds good to me. I had been having issues with doing that, but maybe it was the loop that was constructed poorly. – TheHouseplant May 3 '14 at 6:41
    
Try that. If you got any error then post it. I'll help you. – Jay Shukla May 3 '14 at 6:43
    
Went ahead and updated my post with the $http including the loop and the second $http call. It doesn't actually look like it's trying the second $http call. Fiddler isn't picking up any chatter and I should be seeing quite a bit if it does one for each news item. – TheHouseplant May 3 '14 at 7:02
    
First thing I can see is off is the name spaced item.itemid in your json it's called ID not itemID. Second it's missing the $scope part as well... – Sten Muchow May 3 '14 at 7:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.