javascript - Updating tab badge dynamically on Ionic 2 - Stack Overflow

I wish to update dynamically a badge value, when I click on a button.tabs.html...<ion-tab [root]=&qu

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.

Share Improve this question edited Jul 5, 2017 at 15:02 sebaferreras 44.7k11 gold badges119 silver badges137 bronze badges asked Jul 5, 2017 at 14:40 thexerthexer 971 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745597151a4635184.html

相关推荐

  • javascript - Updating tab badge dynamically on Ionic 2 - Stack Overflow

    I wish to update dynamically a badge value, when I click on a button.tabs.html...<ion-tab [root]=&qu

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信