Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing code after router.navigate in Angular2

I wonder if there is a way to execute something after i navigate to a different "view" using angular router.

this.router.navigate(["/search", "1", ""]);
// Everything after navigate does not not get executed.
this.sideFiltersService.discoverFilter(category);
like image 408
Dave Plug Avatar asked Mar 15 '17 11:03

Dave Plug


People also ask

What does router navigate do in angular?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


1 Answers

this.router.navigate returns a promise so you can simply use:

this.router.navigate(["/search", "1", ""]).then(()=>{
  // do whatever you need after navigation succeeds
});
like image 171
gabo bernal Avatar answered Sep 29 '22 04:09

gabo bernal