我正在加载我在Github上找到的文件上传脚本
<script type="text/javascript" src="{% static 'bower_components/angular-file-upload/angular-file-upload.min.js' %}"></script>
并有一个数据导入模块:
(function () {
'use strict';
angular
.module('TestSite.import', [
'TestSite.import.controllers'
]);
angular
.module('TestSite.import.controllers', ['angularFileUpload']);
})();
以及控制器
(function () {
'use strict';
angular
.module('TestSite.import.controllers' , ['angularFileUpload'])
.controller('UploadController', UploadController);
UploadController.$inject = ['$scope','angularFileUpload'];
/**
* @namespace UploadController
*/
function UploadController($scope, FileUploader) {
$scope.uploader = new FileUploader();
};
})();
我收到了错误
Error: [$injector:unpr] Unknown provider: angularFileUploadProvider <- angularFileUpload
我对模块声明和DI感到困惑。为什么我需要两次声明模块TestSite.import.controllers?我正在注入正确的依赖项(angularFileUpload)并仍然得到该错误,还有什么是提供程序以及为什么角度找不到它?
答案 0 :(得分:1)
看起来你混淆了文件上传服务的名称。正确的依赖注入看起来像这样:
angular
.module('TestSite.import.controllers')
.controller('UploadController', UploadController);
UploadController.$inject = ['$scope', 'FileUploader'];
/**
* @namespace UploadController
*/
function UploadController($scope, FileUploader) {
$scope.uploader = new FileUploader();
};
另请注意,您不必重新定义模块TestSite.import.controllers
。要访问已注册的模块,您需要使用getter表示法,这意味着没有依赖项数组作为第二个参数:
.module('TestSite.import.controllers').controller(...);