The problem is that your i
variable changes before all your ajax requests return. Therefore, it will always be equal to the $scope.langs.length - 1
when your callback executes. You're going to want to create a closure around each of your $http
requests. Angular has some built in functionality for just that:
angular.forEach($scope.langs, function(lang){
// Here, the lang object will represent the lang you called the request on for the scope of the function
$http.get($scope.appURL + lang.shortName + '/whatever/you/want', function(res) {
// Do whatever you want with lang here. lang will be the same object you called the request with as it resides in the same 'closure'
window.localStorage.setItem(lang.shortName, JSON.stringify(res.data));
});
});