找不到AngularJs服务

时间:2016-04-10 17:04:30

标签: angularjs

我正在尝试将服务注入控制器,但找不到服务错误:

angular.js:13424 Error: [$injector:unpr] http://errors.angularjs.org/1.5.3/$injector/unpr?p0=<section data-ui-view="" class="view-container {{main.pageTransition.class}} ng-scope" data-ng-animate="1">copeProvider%20%3C-%20%24scope%20%3C-%20authenticationService
    at Error (native)
    at http://localhost:3000/bower_components/angular/angular.min.js:6:416
    at http://localhost:3000/bower_components/angular/angular.min.js:43:7
    at Object.d [as get] (http://localhost:3000/bower_components/angular/angular.min.js:40:270)
    at http://localhost:3000/bower_components/angular/angular.min.js:43:69
    at d (http://localhost:3000/bower_components/angular/angular.min.js:40:270)
    at e (http://localhost:3000/bower_components/angular/angular.min.js:41:1)
    at Object.instantiate (http://localhost:3000/bower_components/angular/angular.min.js:41:364)
    at Object.<anonymous> (http://localhost:3000/bower_components/angular/angular.min.js:42:184)
    at Object.invoke (http://localhost:3000/bower_components/angular/angular.min.js:41:295)

我的控制器:

(function () {
    'use strict';

    angular.module('app.page')
    .controller('authCtrl', ['$scope', '$window', '$location','authenticationService', authCtrl]);


    function authCtrl($scope, $window, $location, authenticationService) {
        $scope.login = function() {
            authenticationService.test();
        }

        $scope.signup = function() {
            $location.url('/')
        }

        $scope.reset =    function() {
            $location.url('/')
        }

        $scope.unlock =    function() {
            $location.url('/')
        }
    }
})();

服务:

(function () {
    'use strict';

    angular.module('app.page')
        .service('authenticationService', ['$scope', '$window', authenticationService]);

    function authenticationService() {
        this.test = function()
        {
            alert('test');
        }
    }
})();

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

服务调用['$scope', '$window', authenticationService]中的注入次数与服务函数()的参数列表不匹配。

为避免这样的错误,我建议您使用$ inject(https://docs.angularjs.org/guide/di#-inject-property-annotation):

(function () {
    'use strict';

    angular.module('app.page').service('authenticationService', authenticationService);

    authenticationService.$inject = [];

    function authenticationService() {
        this.test = function()
        {
            alert('test');
        }
    }
})();