0

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

1 回答 1

0

您的代码中肯定至少有两个错误:

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

格式化此代码有多种方法,可以解决问题的一种方法是:

    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”,如我上面发布的代码所示。

于 2015-04-09T12:34:19.140 回答