Angular/Ionic Call a function on Navigation change — forward/back.

How to call a function on every route change in Angular.

Subscribe to the router events even you can filter them out on the basis of navigation state may be start, end or so, for example —

(This function calls on both: navigate forward or back from another page.)

import { Router, NavigationEnd } from '';({...})
constructor(private router: Router) {
this.router.events.subscribe((ev) => {
if (ev instanceof NavigationEnd) {
/* Your code goes here on every router change */
/* this function calls on navigate forward or back from another page */
}
});

}

Feel free to comment down in the comment box… If I missed anything or anything is incorrect or anything does not works for you :)
Stay connected for more articles.

Stay connected for more articles:

If you wouldn’t mind giving it some claps 👏 👏 since it helped, I’d greatly appreciate it :) Help others find the article, so it can help them!

Always be clapping…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store