Error in injection of angular module dependencies

Asked

Viewed 681 times

0

I have the module of my application:

angular.module('app', ['app.controllers','app.routes','app.services']);

I have my service module:

angular.app('app.services', [])
    .factory('usuarioService', ['$rootScope', 'renderService',
        function($rootScope, renderService){
            // logica do factory
    }]);

angular.module('app.services', [])
    .factory('renderService', ['$http',
        function($http){
            // logica do factory
    }]);

and I have my controller:

angular.module('app.controllers', ['app.services'])
    .controller('meuCtrl',
        ['$scope','$rootScope','usuarioService','renderservice',
            function($scope, $rootScope, usuarioService, renderService){
               // logica do controller
    }]);

But when running the application, I get dependency injection error:

Unknown provider: usuarioServiceProvider <- usuarioService <- meuCtrl

I don’t understand what could be going on, since I do the injection at every appropriate location.

unless I’m making these wrong injections.

PS.: All . JS files are being loaded into index.html, none forgotten.

  • Post your HTML as well.

2 answers

1


You are setting the app.services module twice.

When you pass the dependency array, the angular creates a new module.

Without the array, it takes an existing module. Try to remove the [] in the second definition.

angular.module('app.services')
.factory('renderService', ['$http',
    function($http){
        // logica do factory
    }
]);

1

The way you declared the services, the second is overwriting the first, because when using the "[]" dependency injector inside the module, the angular creates a new module.

I suggest you try it this way.

angular.module('app.services',[]);

var appServices = angular.module('app.services');

appServices.factory('usuarioService', ['$rootScope', 'renderService',
        function($rootScope, renderService){
            // logica do factory
    }]);

appServices.factory('renderService', ['$http', function($http){
            // logica do factory
    }]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.