Ideal holder for AngularJS Global or Utility functions

52 views
Skip to first unread message

Amogh Talpallikar

unread,
Oct 7, 2014, 2:53:15 AM10/7/14
to ang...@googlegroups.com
Given that rootScope and Services cannot be injected into module configuration section,
I am left with two choices :  a constant or a regular provider.

What is a better approach here ?

What is the rationale behind the fact that constants can also have functions but I haven't really seen people using it for this purpose. 

Sander Elias

unread,
Oct 7, 2014, 3:17:39 AM10/7/14
to ang...@googlegroups.com
Amogh,

What is it you are trying to do in the configuration part, that needs all this stuff?

You can indeed use a constant to provide utility functions if you want. I have done this a couple of times, but it isn't
widely used. 

Regards
Sander

Amogh Talpallikar

unread,
Oct 7, 2014, 6:09:29 AM10/7/14
to ang...@googlegroups.com
I have two routes : 1. login   2. others.

Everytime a route changes, I check if I am going to login or other route.
At this point I have to make an API call here to check if I am logged in or not to handle route redirection based upon response.
Now in the config section where I am managing this, I need a URL for an endpoint.
This endpoint is passed to a utility function that adds base path and API version to form the complete URL.

Now this is something that I want to keep as a utility function. I kept it in rootScope initially but then later decided to move it to a service but I couldn't inject it here.

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/1DvikuoanZM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Oct 8, 2014, 1:17:16 AM10/8/14
to ang...@googlegroups.com
Hi Amogh,

A few day's ago I created a sample that does something similar. It is not using config for anything else as the routing.
Have a look at it, perhaps it might help you.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages