Angular 2: 如何在收到订阅响应后调用函数 http.post

Angular 2: How to call a function after get a response from subscribe http.post

我需要在从 http post 请求中获取数据后调用一个方法

服务:request.service.TS

get_categories(number){
 this.http.post( url, body, {headers: headers, withCredentials:true})
    .subscribe( 
      response => {
        this.total = response.json();

      }, error => {
    }
  ); 

}

组件:categories.TS

search_categories() {

this.get_categories(1);
//I need to call a Method here after get the data from response.json() !! e.g.: send_catagories();
}

只有当我更改为:

时才有效

服务:request.service.TS

get_categories(number){
 this.http.post( url, body, {headers: headers, withCredentials:true})
    .subscribe( 
      response => {
        this.total = response.json();
        this.send_catagories(); //here works fine

      }, error => {
    }
  ); 

}

但是我需要在调用 this.get_categories(1); 之后调用组件内部的方法 send_catagories() 像这样

组件:categories.TS

search_categories() {

this.get_categories(1);
this.send_catagories(response);
}

我做错了什么?

您可以将回调函数添加到 get_category(...) 参数列表中。

例如:

 get_categories(number, callback){
 this.http.post( url, body, {headers: headers, withCredentials:true})
    .subscribe( 
      response => {
        this.total = response.json();
        callback(); 

      }, error => {
    }
  ); 

}

然后你可以像这样调用 get_category(...):

this.get_category(1, name_of_function);
get_categories(number){
 return this.http.post( url, body, {headers: headers, withCredentials:true})
      .map(t=>  {
          this.total = t.json();
          return total;
      }).share();
  );     
}

然后

this.get_category(1).subscribe(t=> {
      this.callfunc();
});

将您的 get_categories() 方法更新为 return 总数(包装在一个可观察对象中):

// Note that .subscribe() is gone and I've added a return.
get_categories(number) {
  return this.http.post( url, body, {headers: headers, withCredentials:true})
    .map(response => response.json());
}

search_categories() 中,您可以订阅由 get_categories() 编辑的可观察对象 return(或者您可以通过链接更多 RxJS 运算符来继续转换它):

// send_categories() is now called after get_categories().
search_categories() {
  this.get_categories(1)
    // The .subscribe() method accepts 3 callbacks
    .subscribe(
      // The 1st callback handles the data emitted by the observable.
      // In your case, it's the JSON data extracted from the response.
      // That's where you'll find your total property.
      (jsonData) => {
        this.send_categories(jsonData.total);
      },
      // The 2nd callback handles errors.
      (err) => console.error(err),
      // The 3rd callback handles the "complete" event.
      () => console.log("observable complete")
    );
}

请注意,您最后只订阅一次

正如我在评论中所说,任何可观察对象的 .subscribe() 方法都接受 3 个回调,如下所示:

obs.subscribe(
  nextCallback,
  errorCallback,
  completeCallback
);

必须按此顺序传递。你不必通过所有三个。很多时候只有 nextCallback 被实现:

obs.subscribe(nextCallback);

您可以将 lambda 表达式编码为订阅方法的第三个参数(完成时)。这里我re-set将departmentModel变量设为默认值。

saveData(data:DepartmentModel){
  return this.ds.sendDepartmentOnSubmit(data).
  subscribe(response=>this.status=response,
   ()=>{},
   ()=>this.departmentModel={DepartmentId:0});
}

您可以使用新的 Subject 来做到这一点:

打字稿:

let subject = new Subject();

get_categories(...) {
   this.http.post(...).subscribe( 
      (response) => {
         this.total = response.json();
         subject.next();
      }
   ); 

   return subject; // can be subscribed as well 
}

get_categories(...).subscribe(
   (response) => {
     // ...
   }
);