[英]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.