1

New to angular. So Here is what I am trying to achieve:

  1. 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']
  2. 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

17
  • i dont see displayForm function anywhere :) Commented Feb 26, 2016 at 22:23
  • @AlexRumbaNicked I don't have it yet, since first the getAlbumTracks itself doesn't work so didn't want to throw it here :) Commented Feb 26, 2016 at 22:25
  • 2
    @noobcoder You will probably get an accurate answer much faster if you create a sample project in JS Fiddle or Plunkr. Looking at the code you posted it's clear that important parts are missing, so it's hard to give an answer that is not a guess. Commented Feb 26, 2016 at 22:30
  • 1
    @noobcoder you are right. That part of the code would be mocked by replacing the $http.get() calls with $q.when(["simulated result"]). Commented Feb 26, 2016 at 22:35
  • 1
    Let us continue this discussion in chat. Commented Feb 26, 2016 at 22:44

2 Answers 2

1

The problem was solved by one line answer.

changing:

vm.albums.tracks = getAlbumTracks; to vm.getAlbumTracks = getAlbumTracks;

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Alex Rumba Nicked for offering to have a private chat and figuring out the goof up. Works fine now.
1

While calling function of controller you should use controller alias before them. Like they are also belongs to this context of controller.

ng-click="vm.getAlbumTracks(album)"
ng-click="vm.displayForm(track)"

1 Comment

Thanks, I realized and I did that. Ill update my post. But it still didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.