Javascript $injector无法解析所需的依赖项
我试图养成按照LIFT协议(Locate、identification、Flat、Try(Dry))构建角度项目的习惯,但是我很难解决来自其他文件的依赖关系 我有以下工厂:Javascript $injector无法解析所需的依赖项,javascript,angularjs,Javascript,Angularjs,我试图养成按照LIFT协议(Locate、identification、Flat、Try(Dry))构建角度项目的习惯,但是我很难解决来自其他文件的依赖关系 我有以下工厂: (function () { 'use strict'; angular .module('CBPWidget', []) .factory('apiManufacturers', apiManufacturers); function apiManufactur
(function () {
'use strict';
angular
.module('CBPWidget', [])
.factory('apiManufacturers', apiManufacturers);
function apiManufacturers () {
function hello () {
return 'hello';
}
return {
hello: hello
};
}
})();
以及以下控制器:
(function () {
'use strict';
angular
.module('CBPWidget', [])
.controller('stepOneController', stepOneController);
stepOneController.$inject = ['$scope', 'apiManufacturers'];
function stepOneController ($scope, apiManufacturers) {
$scope.step = 'step1';
console.log(apiManufacturers.hello);
}
})();
并引发以下错误:
Error: [$injector:unpr] Unknown provider: apiManufacturersProvider <- apiManufacturers <- stepOneController
错误:[$injector:unpr]未知提供程序:apiManufacturersProvider在这里,您要创建两次CBPWidget模块。
angular.module('CBPWidget',[])
用于创建模块和
angular.module('CBPWidget')
用于获取已创建的模块。
因此,用以下代码替换控制器代码:
(function () {
'use strict';
angular
.module('CBPWidget')//now you are getting CBPWidget module
.controller('stepOneController', stepOneController);
stepOneController.$inject = ['$scope', 'apiManufacturers'];
function stepOneController ($scope, apiManufacturers) {
$scope.step = 'step1';
console.log(apiManufacturers.hello);
}
})();
您的angular.module('CBPWidget',[])
块代码正在重新定义angular应用程序,它正在刷新与之相关联的apimemployers
服务,并在其中定义控制器。您不应该这样做,您应该使用已经定义的现有模块
代码
angular
.module('CBPWidget') //don't overide app here use existing
.controller('stepOneController', stepOneController);
从中,你会发现
.module('CBPWidget', [])
不同于
.module('CBPWidget')
后者是您需要引用的模块,前者用于定义一个模块。在所有情况下,除非您首先定义它,否则都应该使用后一种形式