onEnter hook executed multiple times
This is a:
- [x] Bug Report
- [ ] Feature Request
- [ ] General Query
I think this is a bug, but maybe there's a reason for it.
See http://plnkr.co/edit/6EVBkon3JFHdkGXgMwVs?p=preview
If you open the console, you can see some log output. You can see that both the wildcard onEnter hook and the explicity to: 'foo' onEnter hook are called twice when you click the link to the foo route.
Why is this the case? You'd expect this hook to only be called once per transition? This is the log output:
Transition #0 r0: Started -> "Transition#0( ''{} -> 'foo'{} )"
onEnter
onEnter to FOO
onEnter
onEnter to FOO
Transition #0 r0: <- Success "Transition#0( ''{} -> 'foo'{} )", final state: foo
In my production app it's even worse, with the same onEnter hook being called 4 times in the same transition cycle. I'm really curious why.
I couldn't figure out why it's called multiple times, but I discovered that it's the exact same transition object passed to the hook every time, so I applied a property to it once the hook finished and added a check at the start of the hook to check for the presence of this property and not execute the code again.
It only seems to happen for the onEnter hook in particular, the other hooks are called only once:

I think the onEnter hook is called for every single state in the hierarchy, e.g. parent route -> child route -> grandchild route. Is this the case?
If so, why? It seems rather redundant, as you only really are entering one state, which may or may not have a parent state. Moreover, it's being called with the exact same transition object, further adding to the redundancy, as nothing seems to be different from one call to the next. And it's strange that this hook is the only one exhibiting this behaviour.
If this is intended behaviour, can you somehow distinguish between the different states using the hook criteria? E.g. to ensure the hook is only applied on the grandchild state?
Hmm experiencing the same behaviour and it's a little confusing. Thanks for your solution tip @adamreisnz
I have similar problem whit $transitions.onStart and i cant solve it.
The transition ejecute all hook registered previous
I just ran into this same problem but I came from a slightly different angle. You are correct that onEnter is running for each state in the hierarchy and I believe that was intended and correct behavior. The reason you're not getting what you seem to want is because you created a hook that asks for any transition into the onEnter phase. If you only want the grandchild hook you need to either add the hook directly to the onEnter property in the state definition or create the hook specifically for the state in question.
I also agree with you that there needs to be something specifically for the onEnter/onExit/onRetained hooks that specifies which state it is talking about if you've chosen to skip to an ancestor state. Maybe @nateabele can tell us if adding a $current to the transition object is feasible that will be available only during state level transition hooks referencing the specific state that is being entered/retained/existed?
If you only want the grandchild hook you need to either add the hook directly to the onEnter property in the state definition or create the hook specifically for the state in question.
I was under the impression that I'm doing that, by stating {to: 'foo'} in the onEnter hook rules.
By my understanding, this should only execute this hook for the foo state, not multiple times for each of it's parent states as well.
I haven't tried adding the hook in the onEnter property of the state, will see if that has an effect.
Edit: I can confirm that the onEnter hook when defined as a state property does only get executed once, but of course the problem is that services are not available at that time as it's Angular's configuration phase, so it has limited use.
But if that hook only gets executed once, a hook defined in the run phase with explicit condition {to: 'foo'} should also only execute once in my opinion. The current behaviour, whether intended or not, just seems completely redundant and I don't see a valid use case for it.
Why would you want to execute the same hook several times for what's essentially a single terminal state (with some parent states)?
Is there any update on this issue (@christopherthielen)?
We're seeing the same issue but with the onExit hook. When we're exiting two states, we're getting into the onExit hook twice (fine), but both have the same from and to states (see example Plunker).
I had same problem and my modal opens twice in onEnter event
I prevented duplicate execution of opening the modal with a $state.name condition :
onEnter : function(){
if($state.name ==='myModalState')
{
// open the modal
}
}
Having this issue with onStart as well. Any update here would be helpful.
Getting around it with a similar hack to the above:
transition.checked = isUndefined(transition.checked)
if (!transition.checked) { // do things here }
Still an issue in the latest version of ui-router, just ran into it again
I've the same problem for onStart hook but not all the time. For some pages of the app, it runs only one but for some others, twice (however, it's always the same). And it happens only when doing a browser refresh. Exemple:
- app start -> go to login
- login -> see page of projects ** everything's fine and onStart is run only once per page
- refresh the projects page ** onStart is fired twice
Not sure if it helps but I was able to resolve this issue by excluding the nodes by using the Criteria Object. In my example, I excluded about which was is the only parent. For instance in the @scooper91's plunk (fork w/changes):
$transitions.onExit({exiting: (state) => !['about'].some((i) => i === state.self.name)}, function(transition) {...
While workable, I wish there was a way to easily select just children or parent nodes or something.
This behavior is expected and is by design.
Multiple nested states are entered
In the initial bug report, the onEnter hook is run twice when foo is initially activated. The onEnter hook is a State Level Hook. State level hooks are invoked for each state that is entered/exited/retained (both portal and foo states were entered).
This animation from the transitions guide visually demonstrates this aspect:

The onEnter hook is invoked for each state that is entered during the transition.
Transition hook criteria object
When a transition hook is registered, a criteria object is provided. The criteria object only selects a matching transition. It does not indicate the states that the hook is invoked for.
The second argument is the state
The onEnter, onExit, and onRetain transition hooks take a TransitionStateHookFn callback. The TransitionStateHookFn is a function that takes two arguments: the transition and the state. If you want to run a globally registered onEnter hook only for a specific state, then you should check the second argument in the callback.
@adamreisnz
Only 2 and a half years late, but thanks 😆