How to inject service in module.config

6,231 views
Skip to first unread message

RanUser

unread,
Nov 14, 2012, 2:45:19 AM11/14/12
to AngularJS
I referred to the Dependency Injection section of the online guide but
still do not see the way here:

I have a module "SharedServices" and in the config section need to
inject my "app.user" service, and alias it to appUser. This is my
attempt so far:

angular.module('SharedServices', ["app.user"])
.config(["$httpProvider", "app.user", function ($httpProvider,
appUser) { ...

Any ideas? :)

m48u

unread,
Nov 14, 2012, 3:13:59 AM11/14/12
to ang...@googlegroups.com, ranu...@gmail.com
Hi!

I wrote a own module to extract some functionality to make it available for other applications. 

Therefore I started with my module:

angular.module('my.config', []).value('my.config');
angular.module('my.filters', ['my.config']);
angular.module('my.directives', ['my.config']);
angular.module('my', ['my.filters', 'my.directives', 'my.config']);

So I created a module which is dependant of my.config, my.filters and my.directives and is called "my". After that I was simply able to use this 'library' with dependency injection by doing

var newapp = angular.module("newapplication", ["ui", "my"]);
newapp.value("my.config"), { ...//so I can overwrite/extend 'my' config };

I'm using Angular-UI here as well as my own modules.
Hope that helps

Cheers

Peter Bacon Darwin

unread,
Nov 14, 2012, 4:16:42 AM11/14/12
to ang...@googlegroups.com
Hi RanUser
You can only inject "constants" and "providers" into a config block.  What you are probably looking for is the run block, into which you can inject "services" and "values".

angular.module('myModule', []).run(function(myService) {});

Pete


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.



Venkat

unread,
Nov 14, 2012, 11:10:13 AM11/14/12
to ang...@googlegroups.com
Hi RanUser,

You can check for additional information to Peter's point in the following link.



-Venkat

ranu...@gmail.com

unread,
Nov 14, 2012, 12:51:15 PM11/14/12
to ang...@googlegroups.com

Thanks for all your replies!

Perhaps there is a better way to approach my task. I'm trying to add a default transformRequest for the application. to add an Authorization header from my user object which must be included in all server requests. I'm doing this as a default transformRequest so all my $http services can focus on other things. So I am trying to inject the service "app.service" and have it available to my function as parameter name "appUser":

angular.module('SharedServices', ["app.user"])

.config(function ($httpProvider, appUser) { //is it possible to get appUser here?
var handleAuth = function (data, headersGetter) {
var headers = headersGetter();
headers["Authorization"] = appUser.getAuthHeaderValue();

return data

}

$httpProvider.defaults.transformRequest.push(handleAuth);
})

Wizek

unread,
Nov 14, 2012, 3:27:41 PM11/14/12
to ang...@googlegroups.com, ranu...@gmail.com
+1, I'd also like to inject values/factories into config, or to make all ng modules (like $http) configurable when the values and factories are available.
Reply all
Reply to author
Forward
0 new messages