angularjsangularjs-controllerngresourceangularjs-ng-resource

Using Angular Factory Service


I'm trying to follow Code School's Staying Sharp with Angular Soup to Bits.
I've come to the point where I've created a note factory service and implement the $resource service.

I $scope.notes = Note.query() and then console.log($scope.notes) but nothing appears in my dev tools console of my browser. What am I doing wrong?

My GitHub repo with the latest commit where this is happening can be found here.


Solution

  • The way $resource has implemented $scope.notes = Note.query() will assign data returned from ajax directly to $scope.notes variable once Note.query() promise gets resolved.

    But the way you are doing is, you are expecting to get response from async call as soon as you make, for getting response in console you should wait till that promise gets resolved.

    Note.query().$promise.then(function(response){
        $scope.notes = response;
        console.log($scope.notes);
    });
    

    Update

    The other problem was OP has mistakenly registered multiple instances of the NotesIndexController controller, because of which many controller are getting registered but last one controller instance got binded to view. By correcting all other controller name. This issue has been fixed.