0

I'm sure this is boring - it has bothered me for some time now.

I wonder how AngularJS executes?

AngularJS provides some basics Module's Service, Provider and Factory and functions like app.run() and app.config().

What is the executing order of these modules and functions?

If I want to execute a function before the controller or directive executes where should I place it?

3
  • What do you mean by execute a function before the controller or directive executes? Can you give an example? Commented May 20, 2014 at 7:02
  • example: lets consider if I want to redirect a page before any thing executes. I want to check condition from cookie and redirect to another page. where should I place this function Commented May 20, 2014 at 7:06
  • If you're using angular-route, see if stackoverflow.com/questions/14765719/… helps you. Commented May 20, 2014 at 7:16

1 Answer 1

1

AngularJS first gathers everything. Maybe you have written custom directives and filters and external components, AngularJS will first gather each and every resource. Then, it would try to satisfy all the mentioned and required dependencies. So, if your module depends on any external module then, angular would first, load the external module and then would pass it to dependent module.

Now, for app.run() and app.config () methods.

A function passed to app.run() will be executed when all the modules have been loaded. It means all the modules, including external ones.

And a function passed to app.config() will be executed when current module have been loaded.

If I were you, I would place the redirect code in config() method.

Sign up to request clarification or add additional context in comments.

6 Comments

Wouldn't placing it in run make more sense? App.run() is iirc also run before any controllers, services, etc are fired and it seems a bit more logical to put actual redirect logic in run rather than in config.
As I already mentioned that run() would be called after loading of ALL modules whereas, config is executed after loading of CURRENT module. IMO placing the redirection logic in config would make more sense.
A matter of opinion I guess, to me the config is there to configure the app before starting it, route logic just does not belong there even if it would be faster :) Then again it might not belong in run either depending on what the actual use case is.
I too believe that it depends on use case. Normally, I would use config() method to provide initial data from server. Its all in the use cases. :)
actually I didn't need any route logic. I only need to redirect the page on the bases of a flag which is stored in cookie.I think I should go with the config() but again the issue is that the config() is basically for the configuration of app.. What should I do.?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.