Molkrot
Molkrot

Reputation: 55

AngularJS $inject error in service injection

I'm newbie in Angular, so will be glad if you can help me. I split my logic on 2 files (controllers and services). My services code:

(function () {
    'use strict';    
     angular
        .module('app', [])
        .factory('authservice', authservice);

    authservice.$inject = ['$http', '$logger'];

    function authservice($http, $logger) {
        return {
            signInOwner: signInOwner,
            signUpOwner: signUpOwner
        };

        function signInOwner(owner) {

        }

        function signUpOwner(owner) {

        }
    };
})();

My controller code:

(function () {
  'use strict';

   angular
      .module('app', [])
      .controller('SignUpController', SignUpController);

   SignUpController.$inject = ['authservice'];

   function SignUpController (authservice) {
     var vm = this;
  }
})();

I include my services.js before controller.js but still got error about wrong authservice dependencies

angular.js:14362 Error: [$injector:unpr]

Could you help me, please?

Upvotes: 2

Views: 358

Answers (2)

lealceldeiro
lealceldeiro

Reputation: 14958

In your controller code replace:

angular
      .module('app', [])
      .controller('SignUpController', SignUpController);

for

angular
      .module('app') // notice the lack of [] here!!
      .controller('SignUpController', SignUpController);

This is because with the [] it means you're creating the module and without it it means you're locating the module. Since the module was created when your where creating your service, you don't need to create it again, just locate it.

Upvotes: 1

Mistalis
Mistalis

Reputation: 18269

Using this synthax angular.module('app', []), you are overwriting the module creation. You should use angular.module('app') to retrieve it instead.

[] should be use only once: at the creation of the module.


From the Angular module doc:

Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. Use angular.module('myModule') to retrieve an existing module.

Upvotes: 2

Related Questions