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

我怕爱的太早我们不能终老 提交于 2019-12-13 07:16:27

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/25533589/uncaught-error-injectorunpr-unknown-provider-sessioninjectorprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!