[英]Angular2 service doesn't return result to Observable from http.get to component
我正在嘗試使用get在angular2中設置視圖。 組件和服務
在模板中,我有一個seachbutton,它觸發了組件上的搜索功能
//component
searchResult : SearchData[];
searchData(){
this.search.geSearchResultPart1(this.form.searchterm)
.subscribe(
searchresult => {
this.searchResult = searchresult
console.log( searchresult ); // undefined
},
err => {
console.log(err);
});
}
我在服務中
getSearchResultPart1(searchWord : string) : Observable<SearchData[]> {
let fullURL = 'url=http://domain.nl/searchData.php?q='+searchWord;
return this.http.get(fullURL)
.map((res: Response) => {
res.json().data as SearchData[]
console.log(res.json()); // Shows an array with objects
})
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
我還包括了(以及其他必要的進口品)
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
該服務確實在console.log上顯示了響應,但是不返回數據。 我搜索並跟蹤了多個提示,但沒有結果。 我想我做的不錯,但是忘記了一件小事,但是我不知道是什么。
如果將=>
與{}
,則需要顯式return
return this.http.get(fullURL)
.map((res: Response) => {
res.json().data as SearchData[]
console.log(res.json()); // Shows an array with objects
return res.json();
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.