Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material card clickable

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.

like image 718
LucaA Avatar asked Dec 17 '18 14:12

LucaA


3 Answers

simply use the routerLink

<mat-card-content  routerLink = "path">
like image 108
Sachila Ranawaka Avatar answered Sep 22 '22 23:09

Sachila Ranawaka


<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

like image 22
Nikolai Kiefer Avatar answered Sep 22 '22 23:09

Nikolai Kiefer


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']);
}
like image 26
Karnan Muthukumar Avatar answered Sep 23 '22 23:09

Karnan Muthukumar