2

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.

6
  • What actually you want to achieve? You can have $http call inside loop for each IDs. Commented May 3, 2014 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. Commented May 3, 2014 at 6:41
  • Try that. If you got any error then post it. I'll help you. Commented May 3, 2014 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. Commented May 3, 2014 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... Commented May 3, 2014 at 7:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.