Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 模块不可用,但具有正确的模块名称并加载_Angularjs_Dependency Injection - Fatal编程技术网

Angularjs 模块不可用,但具有正确的模块名称并加载

Angularjs 模块不可用,但具有正确的模块名称并加载,angularjs,dependency-injection,Angularjs,Dependency Injection,我对AngularJS中的依赖注入感到非常困惑。最近,我遇到了一个我无法解决的错误。我怀疑依赖注入中存在误解和误用。 这是我的密码: In file A.js var CustomerModel = angular.module('myCustomer',['ngResource','LoginController']); In file B.js CustomerModel .controller('LoginController', LoginController); 我的错误是:

我对AngularJS中的依赖注入感到非常困惑。最近,我遇到了一个我无法解决的错误。我怀疑依赖注入中存在误解和误用。 这是我的密码:

In file A.js
var CustomerModel = angular.module('myCustomer',['ngResource','LoginController']);
In file B.js
CustomerModel
    .controller('LoginController', LoginController);
我的错误是:

未捕获错误:[$injector:modulerr]未能实例化模块myCustomer,原因是: 错误:[$injector:modulerr]未能实例化模块登录控制器,原因是: 错误:[$injector:nomod]模块“LoginController”不可用!您要么拼错了模块名,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数


根据问题和错误,我提出了一个问题。控制器(LoginController)是否可以注入angularJS中的模块(myCustomer)?这就是造成这种错误的原因吗

LoginController
不是一个模块。它只是一个控制器,所以您不需要依赖于该控制器。只需删除它:

// 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', [
    'ngResource',
]);


// register the controller, not a module
CustomerModel
    .controller('LoginController', LoginController);

如果将控制器作为依赖项传递给模块,则该控制器尚不存在。将文件A更改为
var CustomerModel=angular.module('myCustomer',['ngResource'])
LoginController
不是一个模块。它只是一个控制器,所以您不必设置依赖关系