【问题标题】:: [$injector:unpr] Unknown provider in angularjs can't inject factory into controller: [$injector:unpr] angularjs 中的未知提供者无法将工厂注入控制器
【发布时间】:2014-12-19 01:23:55
【问题描述】:

我有一个未知的提供程序错误,我不知道如何解决它。我认为我的服务、控制器已正确声明。我已经尝试了一切,但它不起作用。我的 photosFactory 工厂不工作。它没有注入控制器。我会很感激任何帮助。

我的 app.js:

 angular.module('starter', ['ionic', 'starter.controllers', 'starter.services']) 

我的 controllers.js :

 angular.module('starter.controllers', [])
 .controller('PlaylistsCtrl', ['$scope', 'photosFactory', '$http', function ($scope, $http, Util, $ionicLoading, $location, photosFactory) {

        $ionicSideMenuDelegate.canDragContent(true);

        $scope.allDeals = [];


        $scope.navigate = function(url){
            $location.path(url);
        };
        photosFactory.getPhotos().success(function(data){
            $scope.allDeals= data;

          });
        }])

我的 services.js :

angular.module('starter.services', [])

         .factory('photosFactory', function($http) {
              return{
                  getPhotos : function() {
                      return $http({
                          url: 'http://www.somecompany.co.uk/bigcapi/feeds/deals/company_id/88',
                          method: 'GET',
                          params: {all: '1', mobileready: 1}
                      })
                  }
              }
          })

【问题讨论】:

  • 不清楚的请修改标题
  • 出现同样的错误,非常令人沮丧。似乎只发生在 Ionic 下。

标签: angularjs dependency-injection angularjs-injector


【解决方案1】:

我认为这是因为您没有在工厂中注入 $http。 试试看

.factory('photosFactory', [ '$http', function($http) {
              return{
                  getPhotos : function() {
                      return $http({
                          url: 'http://www.somecompany.co.uk/bigcapi/feeds/deals/company_id/88',
                          method: 'GET',
                          params: {all: '1', mobileready: 1}
                      })
                  }
              };
          }]);

在声明你的控制器的时候也有问题

['$scope', 'photosFactory', '$http', function ($scope, $http, Util, $ionicLoading, $location, photosFactory) {

顺序很重要,一定要有这样的东西

['$scope', 'photosFactory', '$http', 'Util', '$ionicLoading', '$location', function ($scope, photosFactory, $http, Util, $ionicLoading, $location,) { 

【讨论】:

  • 它仍然显示相同的错误,但感谢您的回复。
【解决方案2】:

嗯,

你只声明了 3 个注射剂,

controller('PlaylistsCtrl', ['$scope', 'photosFactory', '$http', 

但想使用 6 - 这不好。

function ($scope, $http, Util, $ionicLoading, $location, photosFactory)

注意顺序。

【讨论】:

  • 谢谢,但我仍然遇到同样的错误,我认为这很容易做到,只是想将我的 ajax 调用移动到服务,但似乎无法做到。它们通过控制器工作,但代码很糟糕,我无法更改它,因此它们应该在服务中但无法使其工作。
  • 好的,我创建了小 jsfiddle,检查 jsfiddle.net/fdzwqszx - 它可以工作,当然 http 调用会失败,但应用程序会引导。我删除了对 ionic 的依赖。
  • 好的,谢谢,不过我没有在任何地方声明我的 PlaylistsCtrl 控制器。
【解决方案3】:

您没有包含您的 HTML,因此无法确认 - 您是否在索引中包含了您的服务 javascript?

假设您解决了各种导入问题,这似乎是最可能的原因。

【讨论】:

    【解决方案4】:

    实际上你应该只声明一次angular.module('starter.controllers', [])。您应该改用angular.module('starter.controllers')。 (例如angular.module('starter.controllers', []).controller ...

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 2023-03-08
      • 2016-02-18
      • 1970-01-01
      • 2016-08-17
      • 2017-06-14
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多