0

Currently a simple app that contains a controller and a service. It looks like the service loads correctly into the module but the controller isn't picking it up. If I remove the 'Photo' injection from the controller, the code doesn't crash. It's only when I try to inject the service into the controller that I see a problem.

controllers.js file:

var photoViewer= angular.module('photoViewer', ['photoViewer.services']);

photoViewer.controller('PhotoListCtrl', ['$scope', 'Photo', function($scope, Photo) {
    $scope.photos = Photo.getPhotos();
}]);

services.js file:

angular.module('photoViewer.services', [], function($provide) {
    $provide.factory('Photo', ['$resource', function($resource){
        return {
            getPhotos: function() {
                return [
                    {'name': 'test1'},
                    {'name': 'test2'}
                ];
            }
        }
    }]);
});

Html file:

<html ng-app="photoViewer">
<head>
    <title>Photo Viewer</title>
</head>
<body>
    <div ng-controller="PhotoListCtrl" style="width:500px;">
        <div>{{photos.length}} total photos found.</div>
        <ul>
            <li ng-repeat="photo in photos">
                {{$index + 1}} - {{photo.name}}
            </li>
        </ul>
    </div>
    <script src="/js/libs/angular.min.js"></script>
    <script src="/js/services.js"></script>
    <script src="/js/controllers.js"></script>
</body>
</html>

1 Answer 1

1

$provide.factory('Photo', ['$resource', function($resource){ has a dependency on $resource which it seems you haven't included.

Here are the docs on $resource:

http://docs.angularjs.org/api/ngResource.$resource

http://docs.angularjs.org/api/ngResource

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

1 Comment

Thank you! That was indeed the problem.

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.