New to angular. I am trying to call multiple $http get calls and the second call depending on the result and parsed JSON of the first one as follows:
1) Do an $http get request to get a JSON with an array of elements like ["album 1", "album2"]
2) Loop over each item in the array and do a different $http get request to get the track details for that album.
Here is the controller code for the same (incomplete) where I want to achieve this:
var vm = this;
vm.albums = init;
vm.albums.tracks = albumTracks;
vm.newFunction = newFunction;
return init();
return albumTracks ();
function init(){
$http.get('http://localhost:8080/api/albums').then(function(responseData){
// Parse the json data here and display it in the UI
vm.albums = responseData;
$log.debug(angular.toJson(responseData, true));
// For every album, do another get call in the function albumTracks
for(var i=0; i<vm.albums.length; i++){
vm.albums.tracks = [];
vm.albums.tracks.push(albumTracks(vm.albums[i]));
console.log(vm.albums.tracks); // This prints on the console as [undefined]
}
console.log(vm.albums.tracks);
return vm.albums;
})
}
function albumTracks(album){
$http.get('http://localhost:8080/api/albums/'+album).success(function(trackResponse){
//parse each album and get the track list
vm.albums.tracks = trackResponse;
return vm.albums.tracks;
})
}
Here is how each JSON response looks like:
//http://localhost:8080/api/albums/:
[
"the-revenant-original-motion-picture-soundtrack",
"twilight-of-the-ghosts"
]
//http://localhost:8080/api/albums/twilight-of-the-ghosts:
[
{
"fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-01-pinned-to-the-mattress.flac",
"title": "Pinned to the Mattress",
"artists": "Twilight of the Ghosts",
"album": "Twilight of the Ghosts",
"sequenceNumber": 1,
"trackLength": 274
},
{
"fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-02-sinking-slowly-slowly-sinking.flac",
"title": "Sinking Slowly Slowly Sinking",
"artists": "Twilight of the Ghosts",
"album": "Twilight of the Ghosts",
"sequenceNumber": 2,
"trackLength": 270
}
and so on