Reputation: 5731
How can I run a piece of code each time the route changes? I can't seem to find an event to bind the code to.
I need this to happen globally throughout the app.
Upvotes: 2
Views: 1908
Reputation: 7964
Use $routeChangeStart
for Ionic - Route change event.It will Broadcast before a route change. At this point the route services starts resolving all of the dependencies needed for the route change to occur.
The event called $routeChangeSuccess
Broadcasted after a route change has happened successfully. The resolve dependencies are now available in the current
Refer this for more details
How to use $routeChangeStart in ionic?
app.js
.run(function($ionicPlatform, $rootScope, $location) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
//print here
});
})
Upvotes: 7