【发布时间】:2018-03-26 21:31:33
【问题描述】:
我有一组数据,用户可以按状态过滤数据。状态为已提交、未提交、已完成和失败。
为了过滤数据,我向后端 api 发送了一个 get 请求,看起来像这样。
/api/data/?status=uncommited,commmitted
上述调用只会带回未提交或已提交的数据。
我在执行此操作时遇到问题,因为网络选项卡显示我的请求,如下所示:
/api/data/?status=uncommited&status=commmitted
你能看到上面有两个“状态”而不是逗号分隔的 1。
data.service.ts
get(params = null) {
let endpoint = "<removed>/api/v1/jobs"
let filters = new HttpParams({fromObject: params});
return this.apiService.get(endpoint, filters);
}
data.component.ts
this.filterForm = this.fb.group({
status: [],
});
this.filterForm.valueChanges.subscribe((res) => {
this.filters = _.pickBy(res, _.identity);
let params = this.filters;
this.dataService.get(params).subscribe((res) => {
this.dataSource = res.data;
});
});
data.component.html
form fxLayout="row" [formGroup]="filterForm" fxLayoutAlign="space-between center" fxLayoutGap="10px" (change)="filter()">
<mat-form-field>
<mat-select placeholder="Status" formControlName="status" multiple>
<mat-option value="any">Any</mat-option>
<mat-option value="completed">Completed</mat-option>
<mat-option value="uncommitted">Uncommited</mat-option>
<mat-option value="committed">Commited</mat-option>
<mat-option value="failed">Failed</mat-option>
</mat-select>
</mat-form-field>
【问题讨论】:
-
你没有多个参数,只有一个。我猜你想说 let params = this.filters.join(',') ?
-
我有多个参数,我只是给出严格的代码视图,以解决特定问题。