Currently, I am diving into the world of JavaScript and AngularJS. My goal is to utilize values that exist outside of a function, yet I'm struggling with how to effectively access them.
Below is a snippet of my code (AngularJS Controller):
var init = function() {
$http.get('getSomeValues').then(function (res) {
var returnArray = res.data; // An array as a result
for(var i=0; i < returnArray.length; i++) { // Looping through the array
console.log("THIS WORKS AS EXPECTED: ", returnArray[i].value); // Log the value
$http.get('getOtherValues/' + returnArray[i].value).then(function (res) {
console.log("WHAT'S INSIDE: ", returnArray[i].value); // Displays 'undefined' error
});
}
});
};
init();
Essentially, my challenge lies in accessing the returnArray, which is proving to be quite elusive. Is there a better method for me to access these values? Could it possibly be related to the use of '.then(function...' triggering an error?