I’ve searched this for awhile with no clear explanation why this occurs.
Using the standard ionic tabs project, on ‘AppCtrl’ which is attached to the ‘tabs.html’, when I establish an $interval function it’s triggered twice. I know some will say I’m not cancelling it, but I’m just starting the app up, this occurs when running ionic-serve also. Is there something about abstract states that triggers the $scope to trigger twice?
I’ve moved the same code out into a controller on a specific state, ‘tabs.chat’ and it works as expected.
maybe you are starting your interval on some state/view events?
if you listen to $ionic.viewEntered or $stateChangeSuccess --> those functions get called twice:
- for the abstract state itself
- for the visible child state
Yes it was on the .beforeEnter event of the controller. I’ve just moved it outside to a specific view’s controller using:
if (!angular.isDefined(intervalVariable) { //DEFINE HERE };
and that works well, now the $http requests I want are just sending once, thanks for clarifying that about it being called twice.
So @bengtler are all functions/definitions called twice for the ionic.viewEvents() ? I am running several things at this point but that seems inefficient if it’s called twice. I guess I can check if it’s defined and skip it subsequently like I do for the $interval function. What are your thoughts?
they are triggered twice because of the abstract/parent state!
see:
- your abstract state --> Listens to the ionicView events
- child state 1 --> gets opened
- child state 2
If you open a child state the parent states are loaded before so you get the event for the abstract state and then because of the child state.