I'm stuck trying to debug my code, particularly because it seems to be related to a javascript issue.
The problem arises when I attempt to load a local txt file using $http.get (is there another method that might work better?). The goal is to store this content in an array. To troubleshoot, I am currently just pushing a generic string into the array to rule out any issues with the actual txt file itself.
var myArray = [];
$http.get(localFilePath).then(
function(success){
myArray.push("123");
},
function(error){
// other stuff
});
console.log(myArray);
This basic code snippet does not seem to create the desired array format. When I use console.log to view the array in Chrome Dev Tools, it appears fine:
https://i.sstatic.net/NH8FS.png
Despite looking correct, the array actually fails to populate - console.log(myArray.length)
always shows 0.
On the contrary, here's how an expected array should appear using the same syntax of myArray.push("123")
but placed outside the $http.get()
function:
https://i.sstatic.net/jxm4d.png
What exactly sets these two arrays apart and causes the first one to be structured differently when created within the $http.get() function?