I am calling the controller to get the API value. How do I pass the array outside of the $http method?
I need to pass an array, pa[]
, to the $scope.myData = pa;
.
First, console.log(pa) prints the value [10,20,30,40].
Second, console.log(pa) empties array[].
JavaScript
function Ctrl($scope, $http) {
var url = "https://spreadsheets.google.com/feeds/list/0AsXjbsnPIu06dGswZUV4WX/values?alt=json-in-script&callback=angular.callbacks._0";
var pa = [];
$http({
method: 'JSONP',
url: url
}).success(function (data) {
for (i = 0; i < data.feed.entry.length; i++) {
var entry = data.feed.entry[i];
pa.push(entry.gsx$productivity.$t);
console.log(pa); //[10,20,30,40,50]
}
});
console.log(pa) // [] empty array
$scope.myData = pa;
}
How do I get the array outside of the $success callback function?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…