未捕获的错误:[$喷油器:unpr]未知提供商:sessionInjectorProvider [英] Uncaught Error: [$injector:unpr] Unknown provider: sessionInjectorProvider

查看:278
本文介绍了未捕获的错误:[$喷油器:unpr]未知提供商:sessionInjectorProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角JS,环回和的NodeJS,

而在角的应用中实施验证,我得到以下提到的错误

 未捕获的错误:[$喷油器:unpr]未知提供商:sessionInjectorProvider<  -  sessionInjector<  -  $ HTTP<  -  $编译

我正想通过这个文件,但没有帮助。
http://www.webdeveasy.com/interceptors-in-angularjs-和有用的,例如/

当我加入了以下线sessionInjector此错误来了

  angular.module('对myApp',[])。工厂('sessionInjectorProvider',['SessionService',函数(SessionService){
    VAR sessionInjector = {
        要求:功能(配置){
            如果(!SessionService.isAnonymus){
                config.headers ['X会话令牌'] = SessionService.token;
            }
            返回配置;
        }
    };
    返回sessionInjector;
}]);angular.module('对myApp',[])。配置(['$ httpProvider',函数($ httpProvider){
    $ httpProvider.interceptors.push('sessionInjector');
}]);


解决方案

有在您的code确保至少有两个误区:

angular.module('对myApp',[])创建一个模块,而 angular.module('对myApp')调用该模块。这意味着,在您的code年底你又在创建模块,因此失去什么你以前写的。

有不同的方式来格式化这个code,一个会解决这个问题将是:

  angular.module('对myApp',[])
        .factory('sessionInjectorProvider',['SessionService',函数(SessionService){
            VAR sessionInjector = {
                要求:功能(配置){
                    如果(!SessionService.isAnonymus){
                config.headers ['X会话令牌'] = SessionService.token;
                    }
                    返回配置;
                }
            };
            返回sessionInjector;
        }])
        的.config(['$ httpProvider',函数($ httpProvider){
                    $ httpProvider.interceptors.push('sessionInjectorProvider');
        }]);

此外,如前面提到的,你混sessionInjectorProvider'和'sessionInjector' - 你的拦截器应该使用'sessionInjectorProvider'如图所示code我上面贴

I am new to angular js, loopback and nodejs,

While implementing the authentication in the angular app, I am getting the below mentioned error

Uncaught Error: [$injector:unpr] Unknown provider: sessionInjectorProvider <- sessionInjector <- $http <- $compile

I was going through this document, but no help. http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/

This error came when I added the below lines for sessionInjector

angular.module('myApp', []).factory('sessionInjectorProvider', ['SessionService', function(SessionService) {
    var sessionInjector = {
        request: function(config) {
            if (!SessionService.isAnonymus) {
                config.headers['x-session-token'] = SessionService.token;
            }
            return config;
        }
    };
    return sessionInjector;
}]);

angular.module('myApp', []).config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('sessionInjector');
}]);

解决方案

There are for sure at least two errors in your code:

angular.module('myApp', []) creates a module, whereas angular.module('myApp') calls the module. This means that at the end of your code you're creating again the module and hence losing what you had written before.

There are different ways to format this code, one that would solve the problem would be:

    angular.module('myApp', [])
        .factory('sessionInjectorProvider', ['SessionService', function(SessionService) {
            var sessionInjector = {
                request: function(config) {
                    if (!SessionService.isAnonymus) {
                config.headers['x-session-token'] = SessionService.token;
                    }
                    return config;
                }
            };
            return sessionInjector;
        }])
        .config(['$httpProvider', function($httpProvider) {
                    $httpProvider.interceptors.push('sessionInjectorProvider');
        }]);

Also, as mentioned already, you're mixing 'sessionInjectorProvider' and 'sessionInjector' - your interceptor should use 'sessionInjectorProvider' as shown in the code I posted above.

这篇关于未捕获的错误:[$喷油器:unpr]未知提供商:sessionInjectorProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆