I can't figure out what is wrong with my code. It seems to be a javascript issue.
I'm loading a local txt file using $http.get (is there a different way?). I want to push this content into an array. For the sake of testing, I'm just pushing any string just to be sure that it doesn't have to do with the actual txt file.
var myArray = [];
$http.get(localFilePath).then(
function(success){
myArray.push("123");
},
function(error){
// other stuff
});
console.log(myArray);
This simple code will not generate a proper array. Here's a screenshot from Chrome dev tool if I console.log the array created:
Now, this looks like a proper array but it's not. If I console.log(myArray.length)
it returns 0.
Here's instead how a proper array should look like using the same code myArray.push("123")
outside $http.get()
function:
Can someone tell what is the difference between the 2 arrays and why the first one is created differently if I do it inside $http.get() function?