简体   繁体   English

角度依赖关系和提供程序不可用错误

[英]Angular dependencies and provider not available error

I am loading a file upload script I found on Github 我正在加载在Github上找到的文件上传脚本

<script type="text/javascript" src="{% static 'bower_components/angular-file-upload/angular-file-upload.min.js' %}"></script>

and have a data import module : 并有一个数据导入模块:

(function () {
 'use strict';

  angular
    .module('TestSite.import', [
      'TestSite.import.controllers'
    ]);

  angular
    .module('TestSite.import.controllers', ['angularFileUpload']);
})();

as well as a controller 以及控制器

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

  };

})();

And I am getting an error 我得到一个错误

 Error: [$injector:unpr] Unknown provider: angularFileUploadProvider <- angularFileUpload

I am confused about the module declaration and DI. 我对模块声明和DI感到困惑。 Why do I need to declare the module TestSite.import.controllers twice? 为什么我需要两次声明模块TestSite.import.controllers? I am injecting the right dependencies (angularFileUpload) and still getting that error, also what is a provider and why cant angular find it? 我正在注入正确的依赖项(angularFileUpload)并仍然收到该错误,还有什么是提供程序,为什么不能用angular找到它?

Looks like you confused name of the file upload service. 看起来您混淆了文件上传服务的名称。 Correct dependency injection looks like this: 正确的依赖项注入如下所示:

angular
    .module('TestSite.import.controllers')
    .controller('UploadController', UploadController);

UploadController.$inject = ['$scope', 'FileUploader'];

/**
 * @namespace UploadController
 */
function UploadController($scope, FileUploader) {
    $scope.uploader = new FileUploader();
};

Also note, that you don't have to redefine module TestSite.import.controllers . 另请注意,您不必重新定义模块TestSite.import.controllers To access already registered module you need to use getter notation, meaning no dependency array as the second parameter: 要访问已注册的模块,您需要使用getter表示法,这意味着没有依赖项数组作为第二个参数:

.module('TestSite.import.controllers').controller(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM