Angular 不管你是否相信。例如,BehaviorSubject应该产生类似http://address/cards/topbrand?perpage=25collectionId=id1,id2,id2,id4…..许多后端框架都内置了将key=value1&key
Angular 不管你是否相信。例如,BehaviorSubject应该产生类似http://address/cards/topbrand?perpage=25collectionId=id1,id2,id2,id4…..许多后端框架都内置了将key=value1&key,angular,http,typescript,Angular,Http,Typescript,不管你是否相信。例如,BehaviorSubject应该产生类似http://address/cards/topbrand?perpage=25collectionId=id1,id2,id2,id4…..许多后端框架都内置了将key=value1&key=value2解析到一个数组中的支持,而不需要额外的代码。通过使用coma方法,您需要创建一个解析器方法。@Jota.Toledo感谢您指出这一点,我不确定是否应该使用该方法。我已经用这两个选项更新了代码,现在您的答案中仍然有“至少它不是最佳实
不管你是否相信。例如,
BehaviorSubject
应该产生类似http://address/cards/topbrand?perpage=25collectionId=id1,id2,id2,id4…..
许多后端框架都内置了将key=value1&key=value2解析到一个数组中的支持,而不需要额外的代码。通过使用coma方法,您需要创建一个解析器方法。@Jota.Toledo感谢您指出这一点,我不确定是否应该使用该方法。我已经用这两个选项更新了代码,现在您的答案中仍然有“至少它不是最佳实践”。你从哪里弄来的?顺便说一句。我很确定导入“rxjs”要么什么都不做,要么“做得太多了”)我删除了最佳实践部分,并添加了rxjs
,如果用户决定使用它的其他功能,而不是可观察的
。是否包含它取决于用户。即BehaviorSubject
this.selectedItems: SearchItem[]= [
{"id":"abee232","itemName":"Attractions"},
{"id":"b6b1a23","itemName":"Artists"},
{"id":"08a58c8","itemName":"Performing Arts"},
{"id":"444d047","itemName":"Sports"}
];
private addRequestOptions(params: SearchItem[]): RequestOptions {
let options: RequestOptions = new RequestOptions();
options.params = new URLSearchParams();
if (params != null) params.forEach((p: SearchItem) => {
options.params.append("&collectionId", p.id);
})
console.log(options)
return options;
}
sortResults(){
let options: RequestOptions = this.addRequestOptions(this.selectedItems);
this.pendingCardsService.collectionUuidUrl = 'http://address/cards/topbrands?perPage=25', options;
this.pendingCardsBrandCollection();
}
export interface SearchItem {
id?: string;
itemName?: string;
}
'http://address/cards/topbrand?perpage=25&collectionId=idValue&collectionId=idValue&collectionId=idValue'
fetchPendingCardsBrandCollection(ids: SearchItem[], pageSize = '25'):Observable<Card[]> {
const params = new URLSearchParams();
params.set('perPage', pageSize);
ids.forEach(id => params.append('collectionId', id.id));
return this.http.get(this.collectionUuidUrl, {params: params})
.map(this.extractData)
}
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Http, URLSearchParams } from '@angular/http';
export interface SomeInterface {
// Define the properties of the JSON object sent as response from the backend
}
@Injectable()
export class SomeService {
constructor(private _http: Http){}
queryElements(ids: string[] = [], pageSize = 25): Observable<SomeInterface>{
const params = new URLSearchParams();
params.set('perPage', pageSize); // using set: replace the existing value of the query parameter "perPage" if exist.
ids.forEach(id => params.append('collectionId', id)); // using append: allows multiple values for the query parameter "collectionId"
return
this._http.get("http://address/cards/topbrand",{params: params})
.map(r => r.json())
.catch(//handle error);
}
}