Service injection into controller with AngularJS

You need to define the Photo service:

angular.module('myApp.controllers', [])
    .service('Photo', ['$log', function ($log) {

        return {
            query: function() {
                // the query code here.
            }
        };

    }])
    .controller('PhotoCtrl', ['$scope', 'Photo', function ($scope, Photo) {
        $scope.photos = Photo.query();
    }])
    .controller('MyCtrl2', [function() {

    }]);

A couple of references:

  • http://docs.angularjs.org/api/angular.Module
  • http://docs.angularjs.org/api/AUTO.$provide#service

In the above sample code I used parameters aliasing, which I suggest in order to avoid issues when minifying your code.

See also an example here:
AngularJS multiple uses of Controller and rootScope

And a Plunker here:
http://plnkr.co/edit/Bzjruq

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)