问题
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