Service injection into controller with AngularJS

前端 未结 1 1238
自闭症患者
自闭症患者 2020-12-03 04:19

I have written a service in AngularJS, but I can\'t get it to work with the angular-seed way of doing things.

The controller code is as follows:

/*fu         


        
相关标签:
1条回答
  • 2020-12-03 05:06

    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

    0 讨论(0)
提交回复
热议问题