小编Lou*_*ise的帖子

Angular 2:如何在从subscribe http.post获得响应后调用函数

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

service:request.service.TS

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

      }, error => {
    }
  ); 

}
Run Code Online (Sandbox Code Playgroud)

组件: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();
}
Run Code Online (Sandbox Code Playgroud)

只有在我改为:

service: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 => {
    }
  ); 

}
Run Code Online (Sandbox Code Playgroud)

但我需要调用的方法send_catagories()调用后构件的内部this.get_categories(1);这样的

组件:categories.TS

search_categories() { …
Run Code Online (Sandbox Code Playgroud)

javascript subscribe promise typescript angular

24
推荐指数
3
解决办法
10万
查看次数

标签 统计

angular ×1

javascript ×1

promise ×1

subscribe ×1

typescript ×1