2017-04-22 12 views
0
tamGetir(yno){ 
    let yazilan:any; 

    let body={ 
      uyeno:this.uye.uyeno, 
      eposta: this.uye.eposta, 
      sifre: this.uye.sifre, 
      gunlukno: this.defterno, 
      yazino: yno 
      } 
    this.http.post('http://www.gibigo.com/sayfalar/ion_android_gungetir2.php',JSON.stringify(body)) 
      .map(res=>res.json()) 
      .subscribe(data=>{ 

       console.log(data.yazi); 
       yazilan=data.yazi; 
      }); 
    return yazilan; 
} 

"data.yazi"はコンソールで正しく表示されますが、リターン操作は機能しません。返品は完了していません。どのように私はそれを正しい返すことができます。ionic 2、 "return"は機能しません。

+0

私の更新の答えをチェック –

答えて

0

http関数は、Observableを返します。これは非同期です。 subscribeを使用すると、http要求が発生し、observableが返されます。データは応答時に受信されます。

yazilan=data.yazi;返信後に起こっています。 サブスクリプション内のデータを保存するには、クラス変数を使用する必要があります。

あなたの方法は、プロバイダである場合は、マップでHTTP呼び出しを返し、コンポーネントでは、コンポーネントに

tamGetir(yno){ 
    let yazilan:any; 

    let body={ 
      uyeno:this.uye.uyeno, 
      eposta: this.uye.eposta, 
      sifre: this.uye.sifre, 
      gunlukno: this.defterno, 
      yazino: yno 
      } 
    return this.http.post('http://www.gibigo.com/sayfalar/ion_android_gungetir2.php',JSON.stringify(body)) 
      .map(res=>res.json()) 

を購読:

yazi:any;//class variable 

callHttpFunction(){ 
    this.provider.tamGetir(yno) 
    .subscribe(data=>{//call the subsribe 
       console.log(data); 
       this.data_variable =data.yazi; 
       }) 
} 
関連する問題