Angular2 http.get链接

时间:2017-08-18 20:53:42

标签: angular http typescript rxjs

我的服务中有以下http.get链:

constructor(private http:Http) {}

getDetails(sysID:string){

var details;
this.http.get('https://blahURL').map(res => res.json().filter(f => f.id == another.id)[0]).subscribe(
    (data) => details = data,             // Reach here if res.status >= 200 && <= 299
    (err) => console.log(err),            // Reach here if fails
    function(){                           // On completion

      console.log(this);

      this.http.get(...) //<- fails

完成后#34;&#34;函数被触发,但我在控制台中获得的this是一个SafeSubscriber对象,因此this.http的下一个http调用失败。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

使用胖箭头语法保留this值。

替换

function(){  // On completion

  console.log(this);

  this.http.get(...) //<- fails

使用

()=>{ // On completion

  console.log(this);

  this.http.get(...) 

答案 1 :(得分:1)

订阅功能中的第二个this不再引用原始对象。

尝试在初始http调用之前添加对它的引用,例如:

let self = this;

并在订阅回调/错误函数中使用它:

self.http.get(..)

相关问题