简体   繁体   English

未捕获的错误:[$ injector:unpr]未知提供程序:sessionInjectorProvider

[英]Uncaught Error: [$injector:unpr] Unknown provider: sessionInjectorProvider

I am new to angular js, loopback and nodejs, 我是angular js,loopback和nodejs的新手,

While implementing the authentication in the angular app, I am getting the below mentioned error 在angular应用中实施身份验证时,出现以下错误

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/ http://www.webdeveasyeasy.com/interceptors-in-angularjs-and-useful-examples/

This error came when I added the below lines for sessionInjector 当我为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. angular.module('myApp', [])创建一个模块,而angular.module('myApp')调用该模块。 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. 另外,正如已经提到的,您正在混合使用“ sessionInjectorProvider”和“ sessionInjector”-拦截器应使用“ sessionInjectorProvider”,如我上面发布的代码所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的错误:[$ injector:unpr]未知提供程序:$ cookiesProvider &lt;-$ cookies &lt;-YourHttpInterceptor &lt;-$ http &lt;-ng1UIRouter - Uncaught Error: [$injector:unpr] Unknown provider: $cookiesProvider <- $cookies <- YourHttpInterceptor <- $http <- ng1UIRouter 未捕获错误:[$ injector:unpr]未知提供者:storeProvider &lt; - store | Auth0 - AngularJS - Uncaught Error: [$injector:unpr] Unknown provider: storeProvider <- store | Auth0 - AngularJS 获取错误:[$ injector:unpr]未知提供程序:socketProvider &lt;-socket &lt;-LogCtrl - Get error : [$injector:unpr] Unknown provider: socketProvider <- socket <- LogCtrl Meanjs:生成器错误:[$ injector:unpr]未知提供程序:MenusProvider &lt;-菜单 - Meanjs:generator Error: [$injector:unpr] Unknown provider: MenusProvider <- Menus 运行AngularJs项目后,我发现了类似错误的错误:[$ injector:unpr]未知的提供程序 - After running the AngularJs Project, I Found Errors like Error: [$injector:unpr] Unknown provider 添加Angular.js模态会导致错误:[$ injector:unpr] - Adding Angular.js Modal gives Error: [$injector:unpr] 角速度未捕获错误:[$ injector:modulerr] - angular-velocity Uncaught Error: [$injector:modulerr] 未捕获(承诺):错误:没有 GooglePlus 的提供者 - Uncaught (in promise): Error: No provider for GooglePlus 未捕获的错误:[$ injector:modulerr]由于以下原因而无法实例化模块myApp:错误:[$ injector:modulerr] - Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:modulerr] MEAN堆栈中的角度错误-未捕获的错误:$ injector:modulerr - Angular error in MEAN stack - Uncaught Error: $injector:modulerr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM