2017-02-28 6 views
1

私はtabBadge番号イオン2タブビューtabBadgeを更新していないときに、モデルチェンジだけ更新するとき、タブをクリック

<ion-tabs selectedIndex="{{activeTab}}"> 
    <ion-tab [root]="tabThongBaoPage" tabIcon="notifications" [tabBadge]="badge.getBadge()" tabBadgeStyle="danger"></ion-tab> 
</ion-tabs> 

とサービスコントローラーに袋の数を示し、このコードを持って

import { Injectable } from '@angular/core'; 
import { Http } from '@angular/http'; 
import 'rxjs/add/operator/toPromise'; 
import CONST from '../variable'; 
@Injectable() 
export class BadgeSinhService { 
    badge: number = 0; 
    constructor() { 
    } 
    getBadge(): number { 
     return this.badge; 
    } 
    setBadge(badge): number { 
     this.badge = badge; 
     return this.badge; 
    } 
    incrementBadge() { 
     this.badge++ 
     return 
    } 
    decreaseBadge() { 
     if (this.badge > 0) { 
      this.badge-- 
     } 
     return; 
    } 
} 

は私の好きなイベントでボタンをクリックすると、この

ビューに更新
<button ion-button (click)="cong()">Cong</button> 
<button ion-button (click)="tru()">Tru</button> 

cong() { 
     this.badge.incrementBadge() 
    } 
tru() { 
    this.badge.decreaseBadge() 
    } 

tabBadge番号、ボタンがクリックされてクリックイベントが発生するとすぐに

私は通知イベントを待つこのコードを持っています。これは、サーバーがアプリに通知を送信すると起動します。タブのバッジ番号が増加します

push.on('notification', (data) => { 
     console.log('notifi updatei'); 
     this.badge.incrementBadge(); 
     }) 

それは、タブのバッジ番号をインクリメントんが、ビューが更新されていない、私はタブをタップしたときにのみ、それはのようなビューを更新しないのはなぜその後、バッジの数が表示

に更新しますイベントをクリックしますか?

答えて

2

私はビュー

push.on('notification', (data) => { 
     console.log('notifi updatei'); 
     this.badge.incrementBadge(); 
     }) 

push.onは2

角度に属していないかもしれないので、角度の2ゾーンで非同期タスクを作成していない上記何とかコードを更新するお知らせ機能angular2にngZoneを使用して、答えを見つけました

ですが、回答は

this.zone.run(() => { 
// increment badge 
}) 
です
関連する問題