2016-12-25 42 views
1

我使用最新版本的離子2.Ionic2動態設置欄項目

離子版本:2.1.8

我想在側邊欄動態添加的項目。

app.component.ts;

export class MyApp { 

    pages: Array<{title: string, component: any, icon: any }>; 

    this.pages = [ 
    { title: 'Dashboard', component: DashboardComponent, icon: 'ios-home-outline' }, 
    { title: 'Complaints', component: ComplaintPage, icon: 'ios-sad-outline' }, 
    { title: 'Suggestions', component: SuggestionPage, icon: 'md-bulb' }, 
    { title: 'Appreciations', component: AppreciationPage, icon: 'ios-thumbs-up-outline' }, 
    { title: 'Poll', component: PollPage, icon: 'ios-stats-outline' }, 
    { title: 'Survey', component: SurveyPage, icon: 'ios-analytics-outline' }, 
    { title: 'ReportIssue', component: ReportIssuePage, icon: 'ios-bug-outline' }, 
    ]; 

} 

app.html

<ion-content> 
    <ion-list> 
    <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)"> 
     <ion-icon item-left [name]="p.icon"></ion-icon> 
     {{p.title}} 
    </button> 
    </ion-list> 
</ion-content> 

我想說明的投訴項目,如果投訴狀態爲 'NEW'。

那麼,是否可以顯示某些特定條件的投訴項目?我嘗試ngIf但它不起作用。

什麼是實現這種功能的最佳方式?

+0

當你試圖'* ngIf'又如何呢? –

+0

這個特定的條件可能會改變,然後在代碼的其他部分?那麼當這種情況發生時應該更新側面菜單? – sebaferreras

回答

1

在組件中有一個變量來檢查對方new的投訴。如果有的話,只需將該變量設置爲true即可。

export class MyApp { 
    this.cmpltStatus; 
    if(newCmplnt){ 
     this.cmpltStatus = true; 
    } else{ 
     this.cmpltStatus = false; 
    } 
} 

現在離子

<ion-content> 
    <ion-list> 
    <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)" *ngIf=(!cmpltStatus && p.title==='Complaints')> 
     <ion-icon item-left [name]="p.icon"></ion-icon> 
     {{p.title}} 
    </button> 
    </ion-list> 
</ion-content> 
+0

它只會在用戶啓動應用程序時才起作用,並且當時它有新的投訴。但當用戶使用該應用程序時添加新投訴時,該功能將無法使用。因爲app.component.ts在應用程序啓動時被調用。並且它的變量在應用程序加載後不會動態改變。我正在尋找解決方案,我們可以隨時從任何其他ts頁面添加項目。 – kabir