Jamie
Jamie

Reputation: 10886

AngularJs service not found

I'm trying to inject a service into a controller but the service is not found error:

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)

My controller:

(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('/')
        }
    }
})();

Service:

(function () {
    'use strict';

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

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

What am I doing wrong here?

Upvotes: 0

Views: 891

Answers (1)

sics
sics

Reputation: 1318

The number of injects in the service call ['$scope', '$window', authenticationService] does not match the argument list of your service function ().

To avoid errors like this, I suggest you to use $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');
        }
    }
})();

Upvotes: 2

Related Questions