AngularJS – Run codes when entering or exiting a state in UI-Router

This post shows how to run codes when you go to and/or leave from a particular state (or route).  One very simple use-case for this is when you want to log something right before a page is displayed and/or before moving to another page.

Software Requirements


Before UI-Router can be used, you have to download it together with the main AngularJS library. Then include them in the main HTML file along with your AngularJS codes.

onEnter and onExit

In your config function and for each state definition, define functions for onEnter and onExit JSON properties.

Testing our SPA

Download the codes


Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert