While working in a for-loop, I encountered an issue where I needed to access the loop variable 'i' from within a callback function but it was not accessible due to closure restrictions. Despite attempting different methods such as using (i)
or call(this, i)
after the callback function, I was unable to resolve the problem.
for (var i = 0; i < $scope.objects.length; i++) {
Priority.find({
filter: {
where: {priority: $scope.selectedPriority[i].priority}
}
}, function (prios) {
Priority.create({"priority": $scope.selectedPriority[i].priority //i is not accessible
}, function (priority) {
$scope.selectedPriority[i].priority = undefined; //i is not accessible
}, function (error) {
console.log(error);
});
}
});