Angular2 Watch For Route Change
Answer :
In the final version of Angular (e.g. Angular 2/4), you can do this
this.router.events.subscribe((event) => { if(event.url) { console.log(event.url); } }); Every time the route changes, events Observable has the info. Click here for docs.
If you are using
"@angular/router": "3.0.0-alpha.7", "@angular/router-deprecated": "2.0.0-rc.2", then
this.router.events.subscribe((event) => { console.log('route changed'); }); Here's what I use in my app. You can subscribe to a Route instance to track changes.
class MyClass { constructor(private router: Router) { router.subscribe((val) => /*detect changes*/) } }
Comments
Post a Comment