2016-08-06 6 views
0

私は、http投稿を実行した後、私のアプリで次のビューに行きたいと思っています。Ionic2のangle2購読後の負荷ビュー

私は3サービスがあります。

  1. サーバサービス - ポストなどを取得、

  2. 都市サービス - - 汎用的なHTTP呼び出しは、サービス1を呼び出すアプリ間で都市リストデータを格納し、データを取得します

  3. ドリルサービス - その都市がサービスからデータを受信すると、市内のリストにドリルは都市についての情報を表示する

角度1では、ページをロードするか、データを持っている何かを行うというokを示す関数(コールバック)を渡します。しかし、httpコール後にこれを達成する方法については、バージョン2ではちょっと残念です。

私は、一般的なポストのために使用したり、要求を取得し、Serverサービスは、上記post(data)都市サービスを呼び出す

post(data){  
var postData = 'data='+JSON.stringify(data); 

return this.http.post(this.server, postData, { 
    headers: this.headers 
}) 
.map(res => res.json());   
} 

市サービスはまた、私はアプリ間でを再利用、データを格納します。

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に電話すると、通話が終了した後でイオンをどのように変更することができますか?

私はコールバックを使用しようとしましたが、渡された関数がnullで動作しません。誤ったTypeScript構文かもしれませんか?/UPDATE

EDIT:

ので、私はそれを動作させるために管理しているが、私は解決策を好きではありません。誰でもこれを改善することができます:

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 
    }); 
    } 

答えて

0

サービスを購読して、サービスが完了したときにページに知らせることができます。新しいページを押します。 this plunkerをご覧ください。

まず、サービスを呼び出すページでは、我々はこのようなサービスに加入:

constructor(private nav: NavController, private service: MyService) {   
     // When the service tells us, we're going to redirect 
     // the user to the Page1 
     this.service.getData.subscribe((receivedData) => { 
      this.nav.push(Page1, { data : receivedData }); 
     });   
    } 

我々はまた、パラメータとして、次のページにサービスからの情報を送ることができることに注意してください。

次に、オブザーバインスタンスでnextメソッドを使用して、すべてのサブスクライバ(この場合はサービスを呼び出したページ)に次のものを実行させるように指示します。

import {Injectable} from '@angular/core'; 
import {Observable} from 'rxjs/Observable'; 

@Injectable() 
export class MyService { 

    private getDataObserver: any; 
    public getData: any; 

    constructor(){ 
    // Initialize the observer 
    this.getDataObserver = null; 
    this.getData = Observable.create(observer => { 
     this.getDataObserver = observer; 
    }); 
    } 

    public getDataFromServer() { 
    // Let's pretend this method gets data from the server 
    setTimeout(() => { 
     this.getDataObserver.next("This is a new message retrieved from the service."); 
    }, 3000); 
    } 
} 

EDIT:

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 
    }); 
    } 

私は(あなたがからNavControllerインスタンスへのアクセス権を持っているので)viewCity(r){...}方法は、あなたのページのいずれかであると仮定します。

私がそこで変更したいのは、同じサービスを使用してデータ(this.cityService.getCity(r))を取得し、そこに保存することだけです(this.cityService.data.city = data.city;)。

なぜあなたはcityServiceからcityService.getCity(r)メソッド内の都市を設定しないと、あなただけのサービスが行われたときにRestaurantPageにリダイレクトする必要があります)

viewCity(r){  
    this.cityService.getCity(r).subscribe((data) => { //call the service and return the http object 
     this.nav.push(RestaurantPage); //load the page and access the service in this component 
    }); 
} 
+0

働くだろうけどgetDataFromServer(それが呼び出されるたびにそのページに行くためにオブザーバーと結合されます。この関数は複数回呼び出され、必ずしもそのページに移動するとは限りません。 – Dezza

+0

ありがとうございますが、getCity(r)は本質的に実行されます:this.http.post(this.server、postData、{ headers:this.headers }) .map(res => res.json())前に購読を処理する。あなたの変更はデータを取得するだけで、サービスには設定されません – Dezza