You can always delay the injection and do it at runtime:
app.factory('something',['$injector', function($injector){
return {
doSomething: function() {
return $injector.get('injectedServiceName').someFn();
},
getSomeInstance: function () {
var Something = $injector.get('Something');
return new Something();
}
};
});
That is always a workaround for circular dependencies and the only way to
go (I know of), if you really need circular dependencies.
For you this would mean:
app.factory('RequestTransformer', [
'config', '$injector',
function(config, $injector) {
return {
request: function(config) {
var user = $injector.get('UserService').get();
if (user) config.headers['x-user-token'] = user.id;
config.headers['x-api-key'] = config.apikey;
config.headers['x-api-version'] = config.apiversion;
return config;
}
}
}
]);
Cheers!
Johnny
On Sunday, October 12, 2014 12:51:56 PM UTC+2, Norman Potter wrote:
>
> Hey peeps
>
> Got a bit of a problem with a circular dependency that I'm struggling with.
>
> Can anyone help me get around this problem.
>
> The error I get is
>
> Circular dependency found: $http <- UserService <- RequestTransformer <-
> $http <- $compile
>
>
> I have a request transformer...
> app.factory('RequestTransformer', [
> 'config', 'UserService',
> function(config, UserService) {
>
> return {
> request: function(config) {
> var user = UserService.get();
> if (user) config.headers['x-user-token'] = user.id;
> config.headers['x-api-key'] = config.apikey;
> config.headers['x-api-version'] = config.apiversion;
> return config;
> }
> }
>
> }
> ]);
>
> and a UserService (truncated for brevity)
>
> app.factory('UserService', [
> '$http', '$q', 'config', 'LocalStorageService', 'AppDataService',
> function($http, $q, config, LocalStorageService, AppDataService) {
>
> return {
> login: function(email, password) {
> var deferred = $q.defer();
> $http({
> method: 'post',
> url: '/login',
> data: {
> email: email,
> password: password
> }
> })
> .success(function(response, status, headers, config) {
> deferred.resolve(angular.fromJson(response));
> })
> .error(function(response, status, headers, config) {
> deferred.reject(angular.fromJson(response));
> });
>
> return deferred.promise;
> }
> ]);
>
>
> Any help greatly appreciated
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.