Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4 - Change state from component not template

Tags:

angular

router

In AngularJS I used ui-router for redirecting inside of my app(changing state).

It has 2 possible options to redirect

  • In template ui-sref='stateName'
  • In controller $state.go()

I just start play with Angular (4) and I found only way how to change route from template with something like:

  • Template routerLink="routePath"

Is there some way as there was in ui-router to change route from component?

like image 732
Andurit Avatar asked Mar 09 '23 12:03

Andurit


1 Answers

constructor(private router:Router) {}

changeRoute() {
  this.router.navigate(...)
  // this.router.navigateByUrl(...)
}

See also https://angular.io/docs/ts/latest/api/router/index/Router-class.html

like image 178
Günter Zöchbauer Avatar answered Mar 20 '23 02:03

Günter Zöchbauer