Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a badge on icon in Ionic 3

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?

like image 406
OshoParth Avatar asked Jul 08 '17 20:07

OshoParth


People also ask

How do I add badges to icon ionic?

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.

What is Ion badge?

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.


2 Answers

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;
}
like image 106
Jack Vial Avatar answered Oct 26 '22 05:10

Jack Vial


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%;
        }
like image 28
mawahidvga Avatar answered Oct 26 '22 03:10

mawahidvga