Hey everyone,
I’m currently trying to create a set of dynamic tabs where certain tabs show or hide, depending on what view you are in within my app. I started off the project using the Tabs starter template, but I feel it’s causing more problems and I’m constantly fighting against the way it wants to do things.The problem is that for some reason my *ngIf is not updating with the latest change to the boolean that it is taking.
Here’s my code so far, some bits cut out for brevity.
I’m using a global file with a behaviorSubject in that looks like this:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/startWith';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class AppGlobals {
public showActionTabs:BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
setShowActionTabs(val){
this.showActionTabs.next(val);
}
getShowActionTabs(): Observable<boolean>{
return this.showActionTabs.asObservable();
}
}
Then within my tabs.ts I have the following:
import { NavController, NavParams } from 'ionic-angular';
import { AppGlobals } from '../../app/globals';
export class TabsPage {
public showTabs: boolean;
constructor(public navCtrl: NavController, public _appGlobals: AppGlobals) {
this._appGlobals.getShowActionTabs().subscribe((value:boolean) => this.showTabs = value);
}
}
Then in my pages that I navigate to I am using something like this to pass in a new true or false:
ionViewWillEnter() {
this._appGlobals.setShowActionTabs(false);
}
Finally in my tabs.html I have a simple *ngIf as follows:
<ion-tab *ngIf="showTabs" (ionSelect)="toggleCompare()" tabTitle="Compare" tabIcon="custom-compare"></ion-tab>
My investigations so far have lead me to believe that the ngIf just isn’t reacting to the new value because it is already checked for a new value before the new value actually is changed in the behaviourSubject. As if I set the tabs to show or hide on my root page beforehand the *ngIf correctly does what it is supposed to. However if I am to .push() a new page or setRoot() to a new page the *ngIf does not react.
Any help/advice as to what is going on is very appreciated.