I need to add a number badge to the cart icon in ionic 3 so that the user gets the update of number of elements in the cart without actually visiting the page i tried to use the code of button and badge together but it was of no use
<button ion-button icon-only (click)="cart()">
<ion-icon name="cart"> <ion-badge item-end>260k</ion-badge></ion-icon>
</button>
Above code displays the badge next to the cart icon but not over it, so is there a way to add badge to the icon itself like we have in notification alert badges?
Adding the code Starting out, open the page you would like to add the Badge Notification to. Once you have opened the file you would like to use it on, add the following to the import section of your file. import { Badge } from '@ionic-native/badge/ngx'; In the Constructor, add private badge: Badge to the list.
shadow. Badges are inline block elements that usually appear near another element. Typically they contain a number or other characters. They can be used as a notification that there are additional items associated with an element and indicate how many items there are.
I think you will have to use some CSS and absolute positioning to place the badge above the left corner of the cart icon.
Something like this:
<button id="cart-btn" ion-button icon-only (click)="cart()">
<ion-icon name="cart"></ion-icon>
<ion-badge id="cart-badge">260k</ion-badge>
</button>
CSS
#cart-btn {
position: relative;
}
#cart-badge {
position: absolute;
top: 0px;
right: 0px;
}
Try this one
Template:
<div>
<button id="notification-button" ion-button clear>
<ion-icon name="notifications">
<ion-badge id="notifications-badge" color="danger">7</ion-badge>
</ion-icon>
</button>
</div>
CSS:
#notification-button {
position: relative;
width: 42px;
top:1px;
right: 1px;
overflow: visible!important;
}
#notifications-badge {
background-color: red;
position: absolute;
top: -3px;
right: -3px;
border-radius: 100%;
}
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