In my angularJS app, I have a loop that increments x times. I'm using a nested HTTP get request within the loop to get an API response for each iteration.
The problem is that the loop counter within the HTTP get function is not getting the correct incrementation value.
// split multiple words
var split = ['stack', 'over'];
// for each word
for(var w = 0; w < split.length; w++) {
// API URL
var api_url = "URL" + split[w]; // correct iteration value, 0, 1, 2, etc...
// get data from API
$http.get(api_url)
// handle successful
.success(function (response) {
console.log(w); // logs 2 everytime! Instead of 0, 1, 2, etc...
});
}
Any ideas on how to solve this?