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:
- Add the
run method in the
- Create a
parent controller that wraps around the whole app? (That feels yuck just thinking that).
- Or do I somehow not
controller that currently has the
$interval in it when that view is exited, if that makes sense.
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.
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.