2016-03-01 52 views
0

我正在學習angularjs2,和/還使用離子2! 離子我創建一個側面菜單:從應用程序Ionic2更新頁面視圖

/app.html:

<ion-content> 
<ion-item-group> 
    <ion-item-divider light>Cats</ion-item-divider> 
    <ion-item *ngFor="#cat of categories" (click)="filter('category', cat.id)">{{cat.name}}</ion-item> 
</ion-item-group> 

當我在這些類別的點擊,我要發送的分類編號,以我的一個網頁(主頁)。

我正在嘗試設置在app.js價值HomePage類:

filter(key, value){ 
HomePage.prototype.setFilters(value); } 

而且在首頁類讀取值:

/home/home.js:

setFilters(value) { 
     this.ViewValue = value; 
     console.log(this.ViewValue); 
    } 

問題是,無法在HomePage視圖中打印該值。 我想有: /home/home.html:

<ion-content class="getting-started"> 
    Selected category: 
    {{ViewValue}} 
</ion-content> 

當我在左邊菜單中的任何類別單擊,在ViewValue不會改變。 當我運行console.log時,我可以獲取我的類別,但它不會在視圖中更改。

我在做什麼錯了? 還是有更好的方法來做到這一點?

回答

0

這是如何在使用訂閱模式的TypeScript中完成的。有可能與純JavaScript代碼差異:

export class FilterEventService extends Subject<any> { 
    constructor() { 
    super(); 
    } 
    valueChanged(value:any){ 
    super.next(value); 
    } 
} 

在你@App組件添加服務:

@App({ 
    providers: [FilterEventService] 
}) 
export class AppComponent{ 
    constructor(@Inject(FilterEventService) private filterEventService:FilterEventService){} 
    filter(key:string, value:any){ 
    this.filterEventService.valueChanged(value); 
    } 
} 

在您的家:

export class HomeComponent { 
    constructor(@Inject(FilterEventService) private filterEventService:FilterEventService) { 
    filterEventService.subscribe((value) => { 
     this.ViewValue = value; 
     console.log(this.ViewValue); 
    }); 
    } 
} 
1

我建議使用導航控制器導航到通過導航參數傳遞類別ID的新頁面。事情是這樣的:

nav.push(HomePage, {categoryId: 123}); 

,並在主頁:

constructor(nav: NavController, navParms: NavParams) { 
    this.myCategoryId = navParms.data.categoryId; 
}