3

I have a mat-icon and a mat-badge on it containing a number.

I'm looking for removing the mat-badge only if the number is <=0.

I did *ngIf on the entire mat-icon, the result its obvious, it remove both mat-icon and mat-badge.

Here is the code

<mat-icon matBadge="{{matBadge}}" class="icon">shopping_cart</mat-icon>
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Sergi
  • 743
  • 1
  • 9
  • 17

2 Answers2

6

You can use matBadgeHidden input property of matBadge directive

<mat-icon matBadge="{{matBadge}}" class="icon" [matBadgeHidden]="number<=0">shopping_cart</mat-icon>
displayName
  • 986
  • 16
  • 40
2

@displayName answer is so clean, you should go with that but you can also use *ngIf with else incase it ever comes handy.

<mat-icon matBadge="{{matBadge}}" class="icon" *ngIf="matBadge > 0; else matBadgeHidden">shopping_cart</mat-icon>

<ng-template #matBadgeHidden>
<mat-icon class="icon">shopping_cart</mat-icon>
</ng-template>
Abdul-Razak Adam
  • 1,070
  • 11
  • 19
  • its working but does ng-template have some default styles such margin, padding? seems like it have own styles – Sergi Jul 22 '18 at 12:26