提供程序依赖项在配置中不起作用 AngularJS

Provider dependancy not working in Config AngularJS

我的代码如下。以下代码在执行以下代码时显示依赖性错误。任何帮助都会很棒。还需要依赖 Cookies...

错误是

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.15/$injector/modulerr?p0=achieverpayroll&p1…A%2F%2Flocalhost%3A8080%2Fachieverpayroll%2Fjs%2Fangular.min.js%3A17%3A381)

代码app.js

(function(){
    var app=angular.module('achieverpayroll',['ngRoute']);


    app.provider('loginChek',function(){
        this.logFn=function(){
            console.log('test');
        };
    });

    app.config(['$routeProvider', '$httpProvider','loginChekProvider', function($routeProvider, $httpProvider,loginChekProvider){

        loginChekProvider.logFn();

        $routeProvider.when('/home',{
            templateUrl: 'templates/home.html',
            controller: 'categoryController'
        }).
        otherwise({
            redirectTo: '/home'
        });
    }]);
    app.controller('categoryController', function($scope, $http) {

    });

})();

HTML:

<!DOCTYPE html>
<html ng-app="achieverpayroll">
<head>
    <meta charset="ISO-8859-1">
    <META http-equiv="X-UA-Compatible" content="IE=10">
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
    <script src="js/angular.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/angular-route.min.js"></script>
    <script type="text/javascript" src="js/angular-cookies.js"></script>
    <script src="js/app.js" type="text/javascript"></script>

.....

尝试这样声明您的提供商:

app.provider('loginChek',function(){

    this.$get = function(){
        return {
            logFn: function() { console.log('test') }
        };
    };

});

当模块由于某些异常而无法加载时,会发生此错误。

你安装了ngRoute模块了吗?

https://code.angularjs.org/1.3.15/docs/api/ngRoute

每当您收到 angular 错误并且您无法真正解码错误消息的含义时。尝试加载 angular 的非最小版本,这将为您提供更具描述性的错误消息,例如,在您的情况下,您可能会看到类似以下内容:

Uncaught Error: [$injector:modulerr] Failed to instantiate module plunker due to: Error: [$injector:pget] Provider 'loginChek' must define $get factory method.

很明显,您的 provider 没有与之关联的服务构造函数。它只是定义了一个提供者函数 可以在配置阶段 访问,这是不够的。例如:

app.provider('loginChek',function(){
      var loggedIn = false;
        //This is accessible only during the config phase before the
        //service loginCheck is even instantiated
        this.logFn=function(){
            loggedIn = true;
            console.log('test');
        };
        //When you inject loginCheck anywhere else you will get a service instance
        //with one method hasLoggedIn
        this.$get = function(){
          return {
            //You can inject services stuffs here like
            //hasLoggedIn:function($http, $q...)
            hasLoggedIn:function(){
              return loggedIn;
            }
          }
        }
    });

plnkr

文档说:

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

Provider 方法 logFn 无法真正使用任何服务,因为服务尚未实例化(例如,您不能在提供程序函数中直接注入 $http 服务,即 .provider('loginChek',function($http){),但是您可以根据需要注入其他提供者。所以它们一般只用于为您的服务做简单的配置。