I have a question that is very much like the one posed here: How to pass a value to an AngularJS $http success callback
As per Angular.js
The $http legacy promise methods success and error have been deprecated. You should use the standard then method instead.
So, how can one achieve a similar outcome using .then?
In my service, my code is a bit fragmented, like this:
fetchFromAPI: function(key) {
return $http.jsonp(...);
}
And in my controller, I have something like this:
for (var i = 0; i < units.length; i++){
Service.fetchFromAPI(i)
.then(function(response) {
console.log(i);
}, function(response) {
console.log(response);
});
}
However, the console output shows the same number for all iterations due to the for-loop completing before any responses are received. I am struggling to figure out how to achieve a similar result to the .success example without encountering an error stating that my controller is not a function.
The solution offered by Jim Horng is as follows:
$http.get('/plugin/' + key + '/js').success((function(key) {
return function(data) {
console.log(key, data);
}
})(key));