2017-02-15 78 views
9

我需要调用一个方法后,从HTTP POST请求获取数据角2:如何调用一个函数得到响应后订阅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); 
} 

我做错了什么?

+0

'send_catagories()'还使用可观察?如果是,则需要使用'.mergeMap()'运算符将get_categories()中的observable链接到send_categories()中的observable。让我知道你是否需要语法帮助。 – AngularChef

+0

send_catagories()不使用observable,请告诉我语法:return this.http.post(url,body,{headers:headers,withCredentials:true}) .subscribe( response => {this.total_page = response .json(); return this.total_page; },.share() ); } then this.get_category(1).subscribe(response => {this.allFunc(); }); – Louis

+0

明白了。我用正确的语法发布了一个答案。 – AngularChef

回答

15

更新您的get_categories()方法到返回总(包裹在一个可观察):

// 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()返回(或者你可以继续通过链接多个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") 
    ); 
} 

注那你只订阅ONCE,最后。

就像我在评论中说,任何可观察到的.subscribe()方法接受3次回调是这样的:

obs.subscribe(
    nextCallback, 
    errorCallback, 
    completeCallback 
); 

他们必须按以下顺序进行传递。你不必通过这三个。很多时候,只有nextCallback实现:

obs.subscribe(nextCallback); 
+0

谢谢,谢谢,谢谢!它工作完美!只是两个问题,1)在this.send_categories(total){this.myNewTotal = total}里面我如何从“total”参数中提取数据(responde.json)? 2)我在语法中添加了“错误”:.map(response => response.json()),error => { });如果我只需要在错误情况下调用this.send_categories(total),我如何订阅错误? – Louis

+0

嗯。根据你以前的代码,我认为'response.json()** WAS **总数。我的意思是,我命名变量'total',但它包含'response.json()'中的所有内容。你可以任意指定这个变量。我已经更新了我的答案,以更好地反映这一点,并向您展示如何处理错误。 – AngularChef

+0

我可能会错过订阅中的第三个参数,但我想知道是否可以调用一个方法来完成回调? – Winnemucca

3

您可以将回调函数添加到您的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); 
+0

谢谢,我会尝试。但我的想法是使用更专业的方法来获得回应。例如:get_category(1)。然后或可观察,但我不知道如何做到这一点 – Louis

+0

有没有什么不专业的使用回调函数,但适合自己 – Gab

+2

在我看来,使用observables传递回调服务是不是一个好的模式。正如路易斯提到的那样,它使得额外的逻辑和错误处理变得丑陋。 – RobM

1
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(); 
}); 
+0

是的,这是我想要的,我如何保持旧的语法没有“.map”?,例如:返回this.http.post(url,body,{headers:headers,withCredentials:true}) .subscribe ( response => {this.total = response.json(); return total; })。share(); – Louis

+0

(173,69):错误TS2339:'订阅'类型中不存在'订阅'属性。 – Louis

+0

为什么你想要原始语法? FRP是关于合并流创建一个可以订阅的有意义的观察。 – pixelbits

0

可以作为一个lambda表达式作为第三个参数(上完成)码的订阅方法。在这里我重新设置departmentModel变量为默认值。

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