【发布时间】:2016-08-06 16:34:39
【问题描述】:
我希望在运行 http 帖子后转到应用中的下一个视图。
我有 3 项服务:
服务器服务 - 通用 http 调用 - 获取、发布等
城市服务 - 在调用服务 1 并获取数据的应用程序中存储城市列表数据
钻取服务 - 钻取城市列表,一旦收到来自城市服务的数据,就会显示有关城市的信息
在角度 1 中,我将传入一个函数(回调),它表示可以加载页面或在我们拥有数据后执行某些操作。但是,我对如何在 http 调用后实现这一点有点卡在第 2 版中。
我用于一般发布或获取请求的服务器服务:
post(data){
var postData = 'data='+JSON.stringify(data);
return this.http.post(this.server, postData, {
headers: this.headers
})
.map(res => res.json());
}
调用上述 post(data) 城市服务的城市服务还存储我在应用程序中重复使用的数据:
viewCity(send){
this.server.post(send).subscribe((data) => {
this.data.cityData = data.city;
//ONCE HERE I WANT TO THEN LOAD THIS
//ANGULAR ONE THE CALLBACK RUN HERE
});
}
那么,当我从 drill 服务调用 viewCity 时,如何在通话结束后让 ionic 更改视图?
我尝试过使用回调,但传入的函数为 null 并且不起作用。可能不正确的 TypeScript 语法?
编辑/更新:
所以我设法让它工作,但我不喜欢这个解决方案。任何人都可以对此进行改进:
viewCity(r){
this.cityService.getCity(r)
.subscribe((data) => { //call the service and return the http object
this.cityService.data.city = data.city; //set the data back through to the service
this.nav.push(RestaurantPage); //load the page and access the service in this component
});
}
【问题讨论】:
标签: typescript angular ionic2