【发布时间】:2018-08-01 21:54:34
【问题描述】:
所有示例仍然引用 Angular 2 方法,NG6 使用管道在服务中进行数据调用。
我的服务中的这个基本方法获取 API 返回的所有数据:
getPatchGroups(): Observable<PatchGroup[]> {
return this.http
.get(API_URL + 'PatchGroups/ForApp/' + appId)
.pipe(
map(this.extractData),
catchError(this.handleError)
);
}
现在,我需要将其拆分,以便我有一个返回任何 id 为 -1 的调用和一个获取其他所有内容的调用。我需要在页面的不同区域显示这两组数据。
getUngroupedPatchGroups(): Observable<PatchGroup[]> {
return this.http
.get(API_URL + 'PatchGroups/ForApp/' + appId)
.pipe(
map(this.extractData), // where id < 0
catchError(this.handleError)
);
}
getGroupedPatchGroups(): Observable<PatchGroup[]> {
return this.http
.get(API_URL + 'PatchGroups/ForApp/' + appId)
.pipe(
map(this.extractData), // where id > 0
catchError(this.handleError)
);
}
在组件中过滤感觉很脏,但这是一种方法……创建一个自定义管道并应用于 ngFor。性能等是否会更好地创建两个函数并对每个函数应用过滤器? NG 6.1.1 的正确格式是什么?
编辑 如果我将过滤器添加到 extraData,我会得到我期望的结果。但是,不推荐使用响应,并且主体是影子命名的?我该如何解决这个问题?
private extractData(resp: Response) {
const body = resp.json();
return body.filter(body => body.id > 0);
}
【问题讨论】:
-
@peinearydevelopment 有趣的信息,但我仍然无法弄清楚如何根据返回的行的数据列之一中的值做出反应。 API 返回包含 7 个属性的记录,我如何根据其中一个属性的值做出反应?这些例子似乎都表明记录只有一个属性。
标签: angular typescript