pipi pipi -4 years ago 212
AngularJS Question

Module is not available,but with correct module name and load it

I am so confused to dependency injection in AngularJS. Recently, I meet an error that I can not solve it. I suspect something misunderstanding and misuse in dependency injection.
Here is my code:

In file A.js
var CustomerModel = angular.module('myCustomer',['ngResource','LoginController']);
In file B.js
.controller('LoginController', LoginController);

My error is:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myCustomer due to:
Error: [$injector:modulerr] Failed to instantiate module LoginController due to:
Error: [$injector:nomod] Module 'LoginController' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

According to the problem and error, I come out with a question. Can a controller(LoginController) is injected in a module(myCustomer) in angularJS? Is this the reason that cause such error?

Answer Source

LoginController is not a module. It is just a controller, so you don't need to lay a dependency on that one. Just remove it:

// only lay dependency on other angular modules
// angular modules are the ones that are registered as 'angular.module('module name', [deps or empty array])'
var CustomerModel = angular.module('myCustomer', [

// register the controller, not a module
    .controller('LoginController', LoginController);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download