I wish to update dynamically a badge value, when I click on a button.
tabs.html
...
<ion-tab [root]="tab1Root" tabTitle="Product" tabIcon="search"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Cart" tabIcon="cart" tabBadge="{{cartCount}}" tabBadgeStyle="danger"></ion-tab>
...
tabs.ts
export class TabsPage {
...
cartCount = 0;
tab1Root = ProductPage;
tab2Root = CartPage;
...
}
product.html
<button ion-button full (click)="updateCart('add', p.id)">Buy Now</button>
product.ts
export class ProductPage {
...
updateCart(action, id) {
let cartCount = 1;
let alert = this.alertCtrl.create({
title: 'Success!',
subTitle: 'You have added 1 product to the cart.',
buttons: ['OK']
});
alert.present();
}
...
}
As I tought, let cartCount = 1;
does nothing. I've searched for a solution, but most of them are for Ionic 1 or AngularJS, which didn't helped me at all.
I wish to update dynamically a badge value, when I click on a button.
tabs.html
...
<ion-tab [root]="tab1Root" tabTitle="Product" tabIcon="search"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Cart" tabIcon="cart" tabBadge="{{cartCount}}" tabBadgeStyle="danger"></ion-tab>
...
tabs.ts
export class TabsPage {
...
cartCount = 0;
tab1Root = ProductPage;
tab2Root = CartPage;
...
}
product.html
<button ion-button full (click)="updateCart('add', p.id)">Buy Now</button>
product.ts
export class ProductPage {
...
updateCart(action, id) {
let cartCount = 1;
let alert = this.alertCtrl.create({
title: 'Success!',
subTitle: 'You have added 1 product to the cart.',
buttons: ['OK']
});
alert.present();
}
...
}
As I tought, let cartCount = 1;
does nothing. I've searched for a solution, but most of them are for Ionic 1 or AngularJS, which didn't helped me at all.
1 Answer
Reset to default 6You could use Ionic events for that. Please take a look at this working plunker. Like you can see there, in the first tab we publish an event when the badge needs to be updated:
import { Component } from '@angular/core';
import { Events } from 'ionic-angular';
@Component({..})
export class FirstTabPage {
private count: number = 0;
constructor(public events: Events) {}
public updateTabBadge(): void {
this.events.publish('cart:updated', ++this.count);
}
}
And then we subscribe to that event in the page that contains both tabs:
import { Component } from '@angular/core';
import { Events } from 'ionic-angular';
@Component({...})
export class FirstTabPage {
private count: number = 0;
constructor(public events: Events) {}
public updateTabBadge(): void {
this.events.publish('cart:updated', ++this.count);
}
}
And in the view:
<ion-header>
<ion-navbar>
<ion-title>Tabs</ion-title>
</ion-navbar>
</ion-header>
<ion-tabs #myTabs>
<ion-tab [root]="tab1Root" tabTitle="First Tab"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Second Tab" [tabBadge]="cartCount"></ion-tab>
</ion-tabs>
Please also notice that property binding should be used instead of string interpolation to bind the tabBadge
property: [tabBadge]="cartCount"
UPDATE
Just like @skinny_jones mentioned in the ments, you could also use Subjects
/Observables
to achieve the same result. You can find more information in this blog post