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

Ankit Maheshwari
2 min readMay 8, 2020

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 '@angular/router';@Component({...})
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:
https://medium.com/@AnkitMaheshwariIn

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…

--

--