我试图养成按照LIFT协议(定位,识别,平面,尝试(干))构建我的Angular项目的习惯,但我遇到了一些困难解决其他文件的依赖关系。
我有以下工厂:
(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
我的工厂JS文件放在我的HTML中的控制器JS文件上面(将缩小)。
对于我出错的地方的任何建议都会非常感激,因为我是以这种方式构建项目的新手。
答案 0 :(得分:2)
这里你要创建两次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);
}
})();
答案 1 :(得分:0)
您的angular.module('CBPWidget', [])
广告代码正在重新定义角度应用,这正在刷新与之关联的apiManufacturers
服务,&amp;它正在定义控制器。你永远不应该这样做,你应该使用已经定义的现有模块。
<强>代码强>
angular
.module('CBPWidget') //don't overide app here use existing
.controller('stepOneController', stepOneController);
答案 2 :(得分:0)
从documentation for AngularJS,你会发现
.module('CBPWidget', [])
与
不同.module('CBPWidget')
后者是你需要引用一个模块,前者用于定义一个模块。在除了您首次定义之外的所有情况下,您应该使用后一种形式。