2017-02-15 75 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}); 
}