I am new to Angular and I have found no clear answers on this subject no matter what I search for.
This is currently working:
<div class="col-sm" (click)="brandListRoute()">
which directs to this function in my component:
brandListRoute() { this.router.navigate(['explore/brands']); }
So this works perfectly, however when I first tried binding [routerLink]
like this:
<div [routerLink]="['explore/brands']"> </div>
I can click on the div but nothing happens. So my question is, does a [routerLink]
only work on things like anchor tags
and buttons
or is my above logic flawed?
Need to find the best solution for my problem. Thanks so much in advance!
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.
You need to add RouterModule to imports of every @NgModule() where components use any component or directive from (in this case routerLink and <router-outlet> . declarations: [] is to make components, directives, pipes, known inside the current module.
RouterLink for dynamic dataDynamic data or user-defined objects can be passed from Angular version 7.2 using the state object stored in History API. The state value can be provided using the routerLink directive or navigateByURL.
Href is the basic attribute provided by Html to navigate through pages which reloads the page on click. routerLink is the attribute provided by angular to navigate to different components without reloading the page.
Using Router linksAfter Angular v4 we can directly add a routerLink attribute on the anchor tag or button. Consider the following template, where routerLink attribute added to the anchor tag. The routerLink directive on the anchor tags give the router control over those elements.
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
<div [routerLink]="['/explore/brands']"> go to this location </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With