Struggling to understand the concept of http requests and promises. I know the http request is asynchronous but have no idea how to implement a promise and return the data to update the $scope variable so that is accessible anywhere.
At the minute the $scope variable only prints out inside '.then'
Basically i'm trying to return the 'season_number' into an array so its displayed as:
Array[
{season_number: 1},
{season_number: 2},
{season_number: 3},
{season_number: 4},
{season_number: 5}
];
Code:
$scope.seasonDetails = [];
var getSeasons = function() {
var data = "";
$http({
method: 'GET',
url: url
})
.then(function(response) {
var tvSeasonDetails = [];
for (var i = 0; i < response.data.seasons.length; i++) {
tvSeasonDetails.push(response.data.seasons[i].season_number)
}
$scope.seasonDetails = tvSeasonDetails;
console.log($scope.seasonDetails); //<----Variable displayed here
})
}
getSeasons();
console.log($scope.seasonDetails); //<---- Empty Array displayed here