Transition Hooks
Transition hooks allow you to execute code at specific points during a transition’s lifecycle. They provide powerful extension points for authentication, authorization, logging, and more.Hook Registry
Hooks are registered using theIHookRegistry interface, implemented by both TransitionService and Transition:
Hook Types
Transition Hooks
FromTransitionHookFn:
onBefore, onStart, onFinish, onSuccess, onError
State Hooks
FromTransitionStateHookFn:
onEnter, onRetain, onExit
Hook Result
FromHookResult:
Hook Lifecycle Phases
onBefore
Runs synchronously before the transition starts. No resolves have been fetched.Best for:
- Quick authentication checks
- Synchronous redirects
- Canceling transitions early
onStart
Runs asynchronously when the transition starts. Good for async operations.Best for:
- Async authentication
- Preloading data
- Showing loading indicators
onExit
Runs when a state is being exited. Receives both transition and the exiting state.Best for:
- Cleanup operations
- Confirmation dialogs
- Saving state before exit
onRetain
Runs when a state is being retained (not exited or entered, but staying active).Best for:
- Updating state when moving between child states
- Refreshing data on parameter changes
onEnter
Runs when a state is being entered.Best for:
- Analytics tracking
- Initializing services
- Setting up subscriptions
onFinish
Runs just before the transition completes. Last chance to cancel or redirect.onSuccess
Runs after the transition successfully completes.onError
Runs if the transition fails.Hook Matching Criteria
FromHookMatchCriteria:
Criterion Types
FromHookMatchCriterion:
String Matching
Function Matching
Match All
Combined Criteria
Hook Options
FromHookRegOptions:
Priority
Higher priority hooks run first:Bind Context
Invoke Limit
Hook Registration
Global Hooks (TransitionService)
Per-Transition Hooks
State Declaration Hooks
Hooks can be defined directly on states:Hook Execution
FromTransitionHook:
Synchronous Execution
Asynchronous Execution
Hook Chaining
FromTransitionHook.invokeHooks():
Common Patterns
Authentication
Authorization
Analytics
Loading Indicators
Unsaved Changes
Deep Linking with Authentication
Best Practices
Use onBefore for sync checks
Use onBefore for sync checks
Use priorities wisely
Use priorities wisely
Clean up hook registrations
Clean up hook registrations
Return promises for async operations
Return promises for async operations
Next Steps
Transitions
Understand the transition lifecycle
States
Define onEnter/onExit in states
URLs & Parameters
Access parameters in hooks
Views
Hook into view loading