javascript - Passing array is not modified with $http Angular service -


i learning angular stuck trying use $http service , json array. have array called names. can show content inside function, outside shows error: typeerror: cannot read property 'name' of undefined. think array not beeing modified properly. don't know why. took code example w3shools.com (http://www.w3schools.com/angular/tryit.asp?filename=try_ng_customers_json). modified little bit. why when want show content of name variable second time error?

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 name again: " + names[0].name); 

$http.get() asynchronous service . try snippet instead checking change on model:

$scope.name = []; $http.get("http://www.w3schools.com/angular/customers.php")     .success(function (response) {     $scope.name = response.records;     console.log("the name is: " + $scope.name[0].name); }); $scope.$watchcollection('name', function (newnames, oldnames) {     if (newnames.length > 0) console.log("the name is: " + $scope.name[0].name); }); 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -