Global Controller?

Just wondering what is the best way to go about implementing something similar to a global controller?

I have an $interval that runs in my app runs every 20s, however I cannot put this code in a controller obviously because when I move to another view the controller’s scope will be destroyed.

So do I:

  1. Add the $interval to run method in the app.js
  2. Create a parent controller that wraps around the whole app? (That feels yuck just thinking that).
  3. Or do I somehow not destroy the controller that currently has the $interval in it when that view is exited, if that makes sense.

Cheers.

I’ve gone with 1. for the moment. It is working fine, but still not sure if it is the best practice.

You may check for the angular official example.

https://docs.angularjs.org/api/ng/service/$interval

Indeed you can pass the interval function to a variable, as long as the variable is not set to undefined and cancel using the $interval, it will keep on running.

So what you may need to concern is that how to close the unused interval instead of worrying about if the interval is canceled after the scope is destroyed.

My solution is to give it to an Angular Service object which may be accessible across the app and I can cancel it and start it at anytime and any place.

Hope this helps.