【问题标题】:use variable this in another function在另一个函数中使用变量 this
【发布时间】:2016-05-17 16:19:20
【问题描述】:

我对 angularjs2 和 ionic 2 中的变量 this 有疑问

我有这个功能:

getAnuncio(id_anuncio){
    console.log(id_anuncio)
    var token=this.local.get('token')._result; 
    var headers= new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Authorization', 'Token '+token);
    datos="id_anuncio="+id_anuncio;
    this.http.post('http://127.0.0.1:8000/envios/getAnuncioPorId/',datos , {
        headers: headers

    })

    .subscribe(success => {
        this.anuncio=success.json();
        console.log("BIENNN");
        console.log(success);
        console.log(this.anuncio);
        return this.anuncio;



    }

}

我从另一个函数调用它:

cargarMapa(){

    this.anuncio=this.getAnuncio(this.envio.anuncio);
    console.log(this.anuncio);
    //console.log(anuncio);
    //this.getOferta(this.envio.oferta);
    //console.log(this.oferta)
}

但是当我尝试登录 this.anuncio 时,它是未定义的。

我需要将数据存储在一个变量中以便在其他函数中使用它。

谁能帮帮我?

这里是代码:https://github.com/p02diada/cyclaClientv2/blob/master/app/pages/sending-details/sending-details.js

【问题讨论】:

    标签: javascript angular ionic2


    【解决方案1】:

    您可以通过这种方式利用do 运算符:

    getAnuncio(id_anuncio){
      console.log(id_anuncio)
      var token=this.local.get('token')._result; 
      var headers= new Headers();
      headers.append('Content-Type', 'application/x-www-form-urlencoded');
      headers.append('Authorization', 'Token '+token);
      datos="id_anuncio="+id_anuncio;
      this.http.post('http://127.0.0.1:8000/envios/getAnuncioPorId/',datos , {
        headers: headers
    
      })
      .map(res => success.json())
      .do(data => {
        this.anuncio = data;
      });
    }
    

    这样你就可以订阅getAnuncio方法之外的observable:

    this.anuncio=this.getAnuncio(this.envio.anuncio).subscribe(
      (anuncio) => {
        // do something
        console.log(anuncio);
      }
    );
    

    不要忘记事情是异步的。

    如果要实现一种缓存,请使用以下内容:

    getAnuncio(id_anuncio){
      if (this.anuncio) {
        return Observable.of(this.anuncio);
      } else {
        var token=this.local.get('token')._result; 
        var headers= new Headers();
        (...)
      }
    }
    

    直接使用this.anuncio是你无法确定数据是否存在...

    【讨论】:

    • 感谢您的回答,但它不起作用。原始异常:TypeError:this.http.post(...).do 不是函数
    • 不客气!您需要包含do 运算符。看到这个问题:stackoverflow.com/questions/34515173/…。将其中的map 替换为do...
    • 我现在已经包含了do 运算符。它不会给我一个错误,但我还不能在函数getAnuncio 之外使用this.anuncio。由于异步,我用setTimeout 尝试过它,但它不起作用。
    • 当我尝试 this.anuncio=this.getAnuncio(this.envio.anuncio).subscribe( (anuncio) => { // do something console.log(anuncio); } ); 它给我一个错误:原始异常:TypeError:无法读取未定义的属性“订阅”
    • 事实上subscribe 返回的是订阅而不是结果。你应该这样:this.getAnuncio(this.envio.anuncio).subscribe( (anuncio) => { this.anuncio = anuncio; } ); .
    猜你喜欢
    • 2017-10-20
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2020-11-27
    • 2014-07-31
    • 1970-01-01
    相关资源
    最近更新 更多