0

I've had a problem in my previous topic, that I couldn't consume my service.

After doing some research I could finally figure out a way to consume my service after all. Still I was wondering why my other approach with the javascript object as method container didn't work out. I have some guesses but can't find an appropriate solution.

Hopefully you guys can lead me on the right path.

controller.js (Working solution)

angular.module('TodoApp.controllers', []).
controller('listCtrl', function ($scope, $location, todoApiService) {
    $scope.todos = todoApiService.query();
});

services.js (Working solution)

angular.module('TodoApp.services', []).
    factory('todoApiService', function ($resource) {
            return $resource('/api/todo/:id', { id: '@id' }, { update: { method: 'PUT' } });
    });

controllers.js (Not working solution)

angular.module('TodoApp.controllers', []).
    controller('listCtrl', function ($scope, $location, todoApiService) {
        $scope.todos = todoApiService.getMyTodos.query();
    });  

services.js (Not working solution)

angular.module('TodoApp.services', []).
    factory('todoApiService', function () {
        var todoApi = {};

        todoApi.getMyTodos = function ($resource) {
            return $resource('/api/todo/:id', { id: '@id' }, { update: { method: 'PUT' } });
        };

    return todoApi;
});
4
  • Does angular's injection kick in for $resource on the 2nd service.js ? Commented Jun 11, 2014 at 12:03
  • Jep, it kicks in at the services.js. Commented Jun 11, 2014 at 12:12
  • Actually, it never really gets into the method "getMyTodos". Couldn't reach my breakpoint at the return $resource statement .. Commented Jun 11, 2014 at 12:20
  • If I set the breakpoint at the todoApiService.query() method and try to step into the method, I get to this update function, while the "clone" object is undefinied oi58.tinypic.com/2z6rqyv.jpg Commented Jun 11, 2014 at 12:32

1 Answer 1

1

You should either:
Inject $resource to your factory function, just like you did in the working version. And then you can remove the $resource as a parameter for getMyTodos.

angular.module('TodoApp.services', []).
    factory('todoApiService', function ($resource) {
        var todoApi = {};

        todoApi.getMyTodos = function () {
            return $resource('/api/todo/:id', { id: '@id' }, { update: { method: 'PUT' } });
        };

    return todoApi;
});

And then from the controller:

angular.module('TodoApp.controllers', []).
    controller('listCtrl', function ($scope, $location, todoApiService) {
        $scope.todos = todoApiService.getMyTodos().query();
    });

Or, you can pass the $resource from the controller to getMyTodos (after injecting it to the controller) - so your controller would look like:

angular.module('TodoApp.controllers', []).
    controller('listCtrl', function ($scope, $location, todoApiService, $resource) {
        $scope.todos = todoApiService.getMyTodos($resource).query();
    });

I didn't check to see that this is working, but it should :)

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

2 Comments

thank you very much, it worked. Kind of makes sense to add the parentheses, even though it looks a bit weird .. ;)
Well, it's a method that returns a resource. So you have to call it in order to get the resource that you want to query. You could implement it using a getter (if the parentheses really annoy you) but that's a different story and it's not quite the correct use-case for a getter.

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.