javascript - Angular 错误: [$injector:unpr] Unknown provider: lineChartProvider <- lineChart <- MainController

标签 javascript jquery angularjs angular-services

不知道为什么,但我的 Controller 无法识别该服务。我已经检查了我在这里缺少的一切。我已将服务文件包含在 HTML 中。它识别其他服务,但不仅仅是这个服务

(function() {
  'use strict';

  angular
    .module('pollyvotes')
    .service('lineChart', lineChart);

    /** @ngInject */
    function lineChart($scope, $http){
      var promise = null;
      return function(){
        if (promise){
          return promise
        } else {
          promise = $http.jsonp('')
          .success(function(data){
            $scope.predictions = data;
         })
          .error( function(data){
            $scope.data = "Request failed";
          })

          return promise;
        }

      }

Controller

(function() {
  'use strict';

  angular
    .module('pollyvotes')
    .controller('MainController', MainController);

  /** @ngInject */
  function MainController($scope, $timeout, lineChart) {

    $scope.photos = [   {id: 'chart-1', name: 'something here',src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"},
                        {id: 'chart-2', name: 'another picture', src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"},
                        {id: 'chart-3', name: 'another picture', src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"},
                        {id: 'chart-4', name: 'another picture', src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"}
                    ];

  }

})();

并声明模块

(function() {
  'use strict';

  angular
    .module('pollyvotes', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize',
                           'ngMessages', 'ngAria', 'ngResource', 'ui.router',
                           'ui.bootstrap',  'akoenig.deckgrid', 'smoothScroll',
                           'ngToast', 'picardy.fontawesome']);

})();

最佳答案

您正在将 $scope 注入(inject)到您的服务中。

文档对此错误的描述如下: https://docs.angularjs.org/error/ $注入(inject)器/unpr

Attempting to inject a scope object into anything that's not a controller or a directive, for example a service, will also throw an Unknown provider: $scopeProvider <- $scope error.

这里有一个很好的答案如何避免这种情况: Injecting $scope into an angular service function()

关于javascript - Angular 错误: [$injector:unpr] Unknown provider: lineChartProvider <- lineChart <- MainController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34947580/

相关文章:

javascript - 在 Django html 模板中包含 html、css、js 小部件

javascript - jQuery 选择器的性能

jquery - 不要在页面刷新时在向导中显示隐藏的 div?

javascript - 使用 JQuery 更改 DIV 中的 javascript 变量

javascript - NG-模型不适用于属性指令

AngularJS 暂停 $location 更改

javascript - 检查是否在javascript中分配了参数

javascript - 分组运行 Jest 测试套件

javascript - 如何使用 token 而不是 azure 连接字符串进行 blob 存储访问

javascript - 将新的 html 元素插入每个 ng-repeat (AngularJS)