[英]Angular 2 http get() funtion is not sending request to backend API after first call
每次將過濾器添加/刪除時,我都需要調用搜索API。 第一次加載組件時,它可以正常工作並將請求發送到支持的控制器。 但是下一次,當我對其應用任何過濾器並再次將請求發送到相同的API時,將調用http.get()函數,但不會將請求發送到支持的控制器。 我檢查了“網絡”標簽,但沒有任何請求。 以下是正確執行以發送API調用的代碼。
constructor(private http: Http, private filterService: FilterService, private dataService: DataService) {
this.subscription = this.filterService.getFilterListObservable()
.subscribe((data) => {
let filterKeysObj = this.createFilterKeyObject(data.activeFilters);
return this.search(filterKeysObj);
});
}
private createFilterKeyObject(activeFilters) {
let filterKeysObj = {};
activeFilters.forEach((element) => {
if (!filterKeysObj[element.type]) {
filterKeysObj[element.type] = [];
}
filterKeysObj[element.type].push(element.item.id || element.item.label);
});
return filterKeysObj;
}
public search(filterKeysObj: Object) {
let params: URLSearchParams = new URLSearchParams();
if (filterKeysObj) {
for (let key in filterKeysObj) {
params.set(key, encodeURIComponent(filterKeysObj[key].join()));
}
}
/*
// This code is to fetch the static data from a data service.
let questionList = this.dataService.questions;
return Observable.of(questionList);
*/
// Http request-
return this.http.get(`/api/tasks/search`, { search: params })
.map((res: Response) => {
return res.json();
});
}
}
您需要訂閱從search
返回的Observable
。 您正在使用可觀察的冷信號 ,因此需要訂閱才能開始序列。 有關熱和冷可觀察物的更多信息,請參見此處 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.