Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the navigate function and routerLink in Angular 2?

I'm following the official Angular 2 tutorial and it uses the navigate function in a very similar way as the routerLink. What is the difference between them? Which to use when?

this.router.navigate(['/detail', this.selectedHero.id]);

[routerLink]="['/detail', hero.id]"
like image 693
user3646717 Avatar asked Apr 16 '17 04:04

user3646717


1 Answers

[routerLink] is an Angular directive that you can use in html for <a> or <button> elements. The directive will listen for click events and navigate to the path created with params provided to routerLink.

this.router.navigate(['/detail', this.selectedHero.id]); is used in your component or service to navigate.

routerLink directive and this.router.navigate should navigate to exactly the same url if the same arguments are specified.

like image 144
Julia Passynkova Avatar answered Oct 06 '22 08:10

Julia Passynkova