admin管理员组文章数量:1323541
I'd like to do somtehing like that:
angular.module('app', []).config(
[ '$httpProvider', 'customAuthService',
($httpProvider, customAuthService) ->
$httpProvider.defaults.transformRequest.push (data) ->
if customAuthService.isLoggedIn
data['api_key'] = {token: @token}
])
According to Angularjs doc, I can't do it in the config
block of my module
, because custom services are not allowed there, nor can I do it in the run
block, because providers like $httpProvider
aren't allowed there:
Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.
How can I do to add some configuration in my $httpProvider
that relies on a home-made service ?
I'd like to do somtehing like that:
angular.module('app', []).config(
[ '$httpProvider', 'customAuthService',
($httpProvider, customAuthService) ->
$httpProvider.defaults.transformRequest.push (data) ->
if customAuthService.isLoggedIn
data['api_key'] = {token: @token}
])
According to Angularjs doc, I can't do it in the config
block of my module
, because custom services are not allowed there, nor can I do it in the run
block, because providers like $httpProvider
aren't allowed there:
Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.
How can I do to add some configuration in my $httpProvider
that relies on a home-made service ?
2 Answers
Reset to default 7It is always possible to get an injector and then the instance of the service inside the callback function ('service locator' style as opposed to having the dependency injected in the config function).
I guess is ok for exceptional cases, although not pretty to use it extensively.
.config([ '$httpProvider', function($httpProvider) {
$httpProvider.defaults.transformRequest.push(function(data) {
var $injector = angular.injector(['app']);
var customAuthService = $injector.get('customAuthService');
// ...
});
}])
But, instead of doing that...
Have you looked at Response interceptors in the $http documentation?
It looks better suited for authentication purpouses and you can get the service injected there.
You can inject it to functions inside of your config as far as I know. I use something similar to intercept requests if they aren't logged in using my auth service.
.config(['$httpProvider',function ($httpProvider) {
var authRequest= ['customAuthService', function(customAuthService) {
if(customAuthService.isLoggedIn){
data['api_key'] = {token: @token};
}
}];
$httpProvider.defaults.transformRequest.push(authRequest);
}]);
本文标签: javascriptAngular Mixing provider and custom service in module39s configrunStack Overflow
版权声明:本文标题:javascript - Angular: Mixing provider and custom service in module's configrun - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122980a2421808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论