2016-03-01 25 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; 
}