Instead of posting in Angular mailing list, I think this may be more of javascript question. Hope the SO community can also give faster response.
I am trying to encapsulate the data in a service and injecting into controller.
angular.module('myApp.services', ['ngResource']).
    factory('Player', function($resource){
        var Player ;
        Player = {
            resource: $resource('/api/Player/:_id', {} )
        };
        return Player
});
function PlayerDetailCtrl(Player, $routeParams, $scope) {
    $scope.resource = Player.resource.get({_id:$routeParams._id});
}
PlayerDetailCtrl.$inject = ['Player', '$routeParams', '$scope'];
It throws an exception
TypeError: Object #<Object> has no method 'query'
$scope.resource = Player.Player.resource.get({_id:$routeParams._id}); also throws error
TypeError: Object #<Object> has no method 'query'
the below works.
angular.module('myApp.services', ['ngResource']).
    factory('Player', function($resource){
        var Player ;
        Player= $resource('/api/Player/:_id', {} )
        return Player
});
function PlayerDetailCtrl(Player, $routeParams, $scope) {
    $scope.resource = Player.Player.get({_id:$routeParams._id});
}
PlayerDetailCtrl.$inject = ['Player', '$routeParams', '$scope'];
my intention is to add more data and method to Player. So how can I make the first (object form) works!
You are creating a factory, this is the atypical way of doing. You don't want to be returning an instance. Angular will give you an instance in your controller.
 factory('Player', function ($resource) { 
    return $resource('/api/Player/:_id', { });
 })
Here is a service I wrote to interact with a cakephp REST service. I wrote it a while back so just take it as an illustration, I may refactor.
 factory('CommentSvc', function ($resource) {
    return $resource('/cakephp/demo_comments/:action/:id/:page/:limit:format', { id:'@id', 'page' : '@page', 'limit': '@limit' }, {
      'initialize' : { method: 'GET', params: { action : 'initialize', format: '.json' }, isArray : true },
      'save': { method: 'POST', params: { action: 'create', format: '.json' } },
      'query' : { method: 'GET', params: { action : 'read', format: '.json' } , isArray : true },
      'update': { method: 'PUT', params: { action: 'update', format: '.json' } },
    });
}).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With