【发布时间】:2017-04-03 21:14:33
【问题描述】:
案例如下:我有一个 ion-tabs 容器,其中包含多个 ion-tab 元素,以及登录到我的应用程序的不同用户。我需要做的是根据记录的用户类型显示或隐藏ion-tab 元素。
问题是我需要动态执行此操作,如果我使用像[show]="variable" 这样的指令,它就不起作用。
tabs.ts
import { Component } from '@angular/core';
import { Page1 } from '../page1/page1';
import { Page2 } from '../page2/page2';
@Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
// this tells the tabs component which Pages
// should be each tab's root Page
tab1Root: any;
tab2Root: any;
tab3Root: any;
tab4Root: any;
tab5Root: any;
variable: any;
constructor(public userData: UserData) {
// get variable from local storage
this.userData.getUser().then((value) => {
if(value.typeLabel == 'Expert') {
this.variable = true;
console.log('1->',value.typeLabel);
}else if(value.typeLabel == 'Client'){
this.variable = false;
console.log('2->',value.typeLabel);
}
});
this.tab1Root = this.variable?Page1:Page2; <-- i want to show specify tabs depending on variable value
this.tab2Root = NotificationsPage;
this.tab3Root = MessagesPage;
this.tab4Root = InstructionsPage;
this.tab5Root = ExpertsPage;
}
}
tabs.html
<ion-tabs>
<ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="tab_title" tabIcon="notifications"></ion-tab>
<ion-tab [root]="tab3Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
<ion-tab [root]="tab4Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
<ion-tab [root]="tab5Root" tabTitle="tab_title" tabIcon="home"></ion-tab>
</ion-tabs>
但总是this.tab1Root返回Page1;
我做错了什么?有人可以帮我吗?
【问题讨论】:
-
你做到了吗?哪个答案对你有帮助?我也是同样的情况。
-
@VivekSinha,使变量 variable = true 以显示选项卡,使其为 false 不显示选项卡。在下方显示
Mohan Gopi响应。
标签: typescript tabs local-storage ionic2