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

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

问题描述

我是 angular js、loopback 和 nodejs 的新手,

I am new to angular js, loopback and nodejs,

在 angular 应用程序中实现身份验证时,出现以下错误

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

我正在阅读此文档,但没有帮助.http://www.webdeveasy.com/interceptors-in-angularjs-和-有用的例子/

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

当我为 sessionInjector 添加以下行时出现此错误

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', []) 创建一个模块,而 angular.module('myApp') 调用该模块.这意味着在您的代码结束时,您将再次创建模块,从而丢失您之前编写的内容.

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');
        }]);

此外,如前所述,您正在混合使用sessionInjectorProvider"和sessionInjector"——您的拦截器应使用sessionInjectorProvider",如我上面发布的代码所示.

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

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

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