AngularJS错误:$ injector:unpr未知提供程序

ric*_*i90 55 javascript angularjs angularjs-service

我正在尝试通过遵循工厂方法文档中的示例来构建自己的服务.我认为我做错了但是因为我继续得到未知的提供程序错误.这是我的应用程序代码,包括声明,配置和工厂定义.

编辑我现在已经添加了所有文件来帮助排除故障

编辑错误的完整详细信息低于getSettings的问题,因为它正在寻找getSettingsProvider而无法找到它

Error: [$injector:unpr] http://errors.angularjs.org/1.2.16/$injector/unpr?    p0=getSettingsProvider%20%3C-%20getSettings
    at Error (native)
    at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:6:450
    at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:35:431
    at Object.c [as get] (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:13)
    at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:35:499
    at c (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:13)
    at d (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:230)
    at Object.instantiate (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:394)
    at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:66:112
    at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:53:14 angular.js:9778
(anonymous function) angular.js:9778
(anonymous function) angular.js:7216
h.$apply angular.js:12512
(anonymous function) angular.js:1382
d angular.js:3869
$b.c angular.js:1380
$b angular.js:1394
Wc angular.js:1307
(anonymous function) angular.js:21459
a angular.js:2509
(anonymous function) angular.js:2780
q angular.js:330
c
Run Code Online (Sandbox Code Playgroud)



这些是我目前在我的应用程序中的所有文件

app.JS

//Initialize angular module include route dependencies

var app = angular.module("selfservice", ['ngRoute']);

app.config(function ($routeProvider) {
   $routeProvider
       .when('/', {
           templateUrl:"partials/login.html",
           controller:"login"
       });
});






app.factory('getSettings', ['$http', '$q', function($http, $q) {
    return function (type) {
        var q = $q.defer();
        $http.get('models/settings.json').success(function (data) {
            q.resolve(function() {
                var settings = jQuery.parseJSON(data);
                return settings[type];
            });
        });

        return q.promise;
    };
}]);
Run Code Online (Sandbox Code Playgroud)

以下是我在控制器中使用此服务的方式

controller.JS

app.controller("globalControl", ['$scope','getSettings', function ($scope,getSettings) {
    var loadSettings = getSettings('global');
    loadSettings.then(function(val) {
        $scope.settings = val;
    });

}]);


app.controller("login", ['$scope', function ($scope) {

    return ""



}]);
Run Code Online (Sandbox Code Playgroud)

directives.js

app.directive('watchResize', function(){
    return {
        restrict: 'M',
        link: function(scope, elem, attr) {
            scope.spacer = (window.innerWidth < 1025) ? '' : 'large-3';
            scope.button = (window.innerWidth < 1025) ? '' : 'large-6';
            angular.element(window).on('resize', function(){
                scope.$apply(function(){
                    scope.spacer = (window.innerWidth < 1025) ? '' : 'large-3';
                    scope.button = (window.innerWidth < 1025) ? '' : 'large-6';
                });
            });
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

如果这是相关的HTML

<html class="no-js" lang="en" ng-app="selfservice" ng-controller="globalControl">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     <title>{{settings.title}}</title>
     <link rel="stylesheet" href="css/app.css" />
    <script src="bower_components/modernizr/modernizr.js"></script>
      <script src="bower_components/angular/angular.min.js"></script>
       <script src="bower_components/angular-route/angular-route.min.js"></script>
       <script src="bower_components/jquery/dist/jquery.min.js"></script>
      <script src="js/app.js"></script>
      <script src="js/controllers.js"></script>
      <script src="js/directives.js"></script>
  </head>
  <body>
    <div id="template">
        <header id="header">
            <img src="{{settings.logo}}" alt="{{settings.logoDescription}}"/>
        </header>

        <div id="view">
            <ng-view></ng-view>
        </div>

    </div>

    <script src="bower_components/foundation/js/foundation.min.js"></script>
        <script>
        //initialize foundation
        $(document).foundation();

    </script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向吗?我已尽力遵循文档,并查看大部分相关问题更深入,更难以理解.这是我第一次创建服务.

Has*_*ash 39

也可能是您错过了在您的页面中包含服务文件的一个流行原因

<script src="myservice.js"></script>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!因为这个我花了几个小时调试. (5认同)
  • 一小时冲浪结果,在这里你去.愚蠢的错误.谢谢. (3认同)

Kun*_*ale 33

您的角度模块需要正确初始化.app需要正确定义和初始化全局对象以注入服务.

请参阅以下示例代码以供参考:

app.js

var app = angular.module('SampleApp',['ngRoute']); //You can inject the dependencies within the square bracket    

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider
    .when('/', {
      templateUrl:"partials/login.html",
      controller:"login"
    });

  $locationProvider
    .html5Mode(true);
}]);

app.factory('getSettings', ['$http', '$q', function($http, $q) {
  return {
    //Code edited to create a function as when you require service it returns object by default so you can't return function directly. That's what understand...
    getSetting: function (type) { 
      var q = $q.defer();
      $http.get('models/settings.json').success(function (data) {
        q.resolve(function() {
          var settings = jQuery.parseJSON(data);
          return settings[type];
        });
      });
      return q.promise;
    }
  }
}]);

app.controller("globalControl", ['$scope','getSettings', function ($scope,getSettings) {
  //Modified the function call for updated service
  var loadSettings = getSettings.getSetting('global');
  loadSettings.then(function(val) {
    $scope.settings = val;
  });
}]);
Run Code Online (Sandbox Code Playgroud)

示例HTML代码应如下所示:

<!DOCTYPE html>
<html>
    <head lang="en">
        <title>Sample Application</title>
    </head>
    <body ng-app="SampleApp" ng-controller="globalControl">
        <div>
            Your UI elements go here
        </div>
        <script src="app.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

请注意,控制器不绑定到HTML标记,而是绑定到body标记.此外,请尝试在HTML页面的末尾包含您的自定义脚本,因为这是出于性能原因而遵循的标准做法.

我希望这能解决你的基本注射问题.

  • 我看不到你在样本中做了什么,而我没有在这个问题中做过和展示,你能更具体点吗? (2认同)
  • 我已用适当的评论更新了答案.我希望这能解决你的问题. (2认同)

mpm*_*mpm 10

app.factory('getSettings', ['$http','$q' /*here!!!*/,function($http, $q) {
Run Code Online (Sandbox Code Playgroud)

你需要声明所有的依赖关系或者没有,你忘了声明$ q.

编辑:

controller.js:登录,不要返回""


Dfr*_*Dfr 9

当一个人意外地将$ scope注入他们的工厂时,也会出现此错误:

angular.module('m', [])
    .factory('util', function ($scope) { // <-- this '$scope' gives 'Unknown provider' when one attempts to inject 'util'
       // ...
    });
Run Code Online (Sandbox Code Playgroud)