1

I have a situation where I am using $http.get in a for loop. I now need to associate the http response to the object that was being iterated in the loop. The problem however is .success is asynchronous, so this does not work:

 for (var i =0; i< monitors.length; i++)
    {

        var url = loginData.apiurl + "/events/index/MonitorId:"+monitors[i].Monitor.Id+".json?page=1";
        console.log ("Monitor event URL:"+url);
        $http.get(url)
            .success (function (data)
            {
                // by the time we reach here, i is out of context,
                // so this is an error

                console.log ("**** EVENT COUNT FOR MONITOR " + monitors[i].Monitor.Id + " IS " + data.pagination.count);
                monitors[i].Monitor.Events = data.pagination.count;
            })
            .error (function (data)
            {

            });


    }

So is it possible for me to pass a variable in the http.get function that returns on success too as a context? If not, how does one do this the Angular way?

user1361529
  • 2,667
  • 29
  • 61

0 Answers0