Angular 依赖项和提供程序不可用错误

Angular dependencies and provider not available error

我正在加载我在 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),但仍然出现该错误,还有什么是提供程序,为什么 angular 找不到它?

您似乎混淆了文件上传服务的名称。正确的依赖注入看起来像这样:

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(...);