New to angular. So Here is what I am trying to achieve:
- Do an $http get request to get a list of albums and show them in the UI as a list of URLs (Example of the response is ['album1', 'album2', 'album3']
- When I click on album 1, I need to make a $http get request for album 1. When I click on album 2, I need to make a $http get request for album 2 and so on and display that response accordingly.
Here is what my code looks like. ( I know that the first get call will be made regardless but the subsequent calls will be on click events.)
// module
(function () {
'use strict';
angular.module('app.fileUploadForm', [
'app.layout',
'ui.router',
'angularFileUpload'
]).config(FileUploadFormConfiguration);
FileUploadFormConfiguration.$inject = ['$stateProvider'];
function FileUploadFormConfiguration($stateProvider) {
$stateProvider.state('fileUploadForm', {
url: '/fileUploadForm',
parent: 'default',
templateUrl: 'app/fileUploadForm/fileUploadForm.html',
controller: 'FileUploadFormController as vm'
});
}
})();
// Controller
(function (){
angular.module('app.fileUploadForm').controller('FileUploadFormController', FileUploadFormController);
FileUploadFormController.$inject = ['$http', '$log', '$scope', '$rootScope', 'APP_CONFIG'];
function FileUploadFormController ($http, $log, $scope, $rootScope, APP_CONFIG){
var vm = this;
vm.albums = init;
vm.albums.tracks = getAlbumTracks;
vm.newFunction = newFunction;
return init();
function init(){
$http.get('http://localhost:8080/api/albums').then(function(responseData){
// Parse the json data here and display it in the UI
$scope.vm.albums = responseData.data;
$log.debug(angular.toJson(responseData, true));
// console.log(vm.albums.tracks);
return vm.albums;
})
}
function getAlbumTracks(album, $scope){
$http.get('http://localhost:8080/api/albums/'+album).success(function(trackResponse){
//parse each album and get the track list
$scope.vm.albums.tracks = vm.albums.tracks.concat(trackResponse);
return vm.albums.tracks;
})
}
}
// HTML code:
<div class="wrapper wrapper-content">
hello!
<div>
<ul>
<li ng-repeat="album in vm.albums"><a href ng-click="vm.getAlbumTracks(album)">{{album}}</a></li>
<ul>
<li ng-repeat="track in vm.albums.tracks"><a href ng-click="vm.displayForm(track)">{{track}}</a></li>
</ul>
</ul>
</div>
I can see the list of albums but the second request to fetch the track info after clicking the album doesn't print anything. Any clue how to achieve that ? Thanks
$http.get()calls with$q.when(["simulated result"]).