2017-04-22 74 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”在控制台中看起来正确,但返回操作不起作用。退货不确定。我如何正确返回它。离子2,“返回”不起作用

+0

检查我的更新答案 –

回答

0

http函数返回Observable。这是异步的。当你使用订阅时,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; 
       }) 
}