Currently diving into Angular and facing a roadblock with the $http service and a JSON array. Within my code, there's an array labeled names. While I'm able to display its content within the function, an error pops up when trying to do so outside: TypeError: Cannot read property 'Name' of undefined. It seems like the array isn't being modified properly, but the reason behind this is unclear to me. I referred to an example on W3shools.com (http://www.w3schools.com/angular/tryit.asp?filename=try_ng_customers_json) and made some modifications. Why am I encountering an error the second time I attempt to display the contents of the name variable?
var names = [];
$http.get("http://www.w3schools.com/angular/customers.php")
.success(function(response) {
names = response.records;
console.log("The name is: " + names[0].Name);
});
console.log("And now the name again: " + names[0].Name);