I want to add a routerlink to a mat-card component to make card clickable. My component is like this:
<mat-card class="card" >
<mat-card-content>
<mat-card-title> {{title}}</mat-card-title>
<mat-card-subtitle> {{subtitle}} </mat-card-subtitle>
</mat-card-content>
</mat-card>
How to accomplish this?
Thanks.
simply use the routerLink
<mat-card-content routerLink = "path">
<mat-card (click)="doStuff()" class="card" >
<mat-card-content>
<mat-card-title> {{title}}</mat-card-title>
<mat-card-subtitle> {{subtitle}} </mat-card-subtitle>
</mat-card-content>
</mat-card>
Then it should handle the click
If you need to any operations before navigate.Best Practices is use click method for handling any operations and navigate,
Html
<mat-card class="card" (click)="navigate()">
<mat-card-content>
<mat-card-title> {{title}}</mat-card-title>
<mat-card-subtitle> {{subtitle}} </mat-card-subtitle>
</mat-card-content>
</mat-card>
Ts
import { Router } from '@angular/router';
constructor(private router:Router){
}
navigate(){
//do your any operations
this.router.navigate(['path']);
//also you can pass like this,
this.router.navigateByURL(['path']);
}
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