My question is quite similar to the question here: How to pass a value to an AngularJS $http success callback
According to Angular.js
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead.
So how would one, do the similar just with .then?
My code is fragmented with something like this in my service
fetchFromAPI: function(key) {
return $http.jsonp(...);
}
And this as my controller
for (var i = 0; i < units.length; i++){
Service.fetchFromAPI(i)
.then(function(response) {
console.log(i);
}, function(response) {
console.log(response);
});
}
But the output from the console is the same number due to the for-loop finishing execution before any of the calls is returned and I can't wrap my head around to write the above similar to the .success example, all my attempts gives me an error that my controller is not a function.
The solution posted by Jim Horng is
$http.get('/plugin/' + key + '/js').success((function(key) {
return function(data) {
console.log(key, data);
}
})(key));