admin管理员组文章数量:1310385
I am learning Angular 2, TypeScript, RxJs, etc. and I am having a issue returning a subset of data inside a service using RxJs and Observable.
I expect the getCars function below to read a json file, parse it and return a slice of the data (offset and count). However, I always get all the data back (I have 200 entities/cars in the file I am testing with).
What am I doing wrong?
EntityService
@Injectable()
export class EntityService {
constructor(private http: Http) { }
getCars(offset: number, count: number): Observable<Car[]> {
return this.http
.get('resources/data/cars.json')
.map(this.extractData)
.skip(offset)
.take(count)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
private handleError(error: any) {
// ...
}
}
cars.json
{
"data":[
{
"vin":"ee8a89d8",
"brand":"Fiat",
"year":1987,
"color":"Maroon"
},
{
"vin":"642b3edc",
"brand":"Renault",
"year":1968,
"color":"White"
}
]
}
I am learning Angular 2, TypeScript, RxJs, etc. and I am having a issue returning a subset of data inside a service using RxJs and Observable.
I expect the getCars function below to read a json file, parse it and return a slice of the data (offset and count). However, I always get all the data back (I have 200 entities/cars in the file I am testing with).
What am I doing wrong?
EntityService
@Injectable()
export class EntityService {
constructor(private http: Http) { }
getCars(offset: number, count: number): Observable<Car[]> {
return this.http
.get('resources/data/cars.json')
.map(this.extractData)
.skip(offset)
.take(count)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
private handleError(error: any) {
// ...
}
}
cars.json
{
"data":[
{
"vin":"ee8a89d8",
"brand":"Fiat",
"year":1987,
"color":"Maroon"
},
{
"vin":"642b3edc",
"brand":"Renault",
"year":1968,
"color":"White"
}
]
}
Share
Improve this question
asked Jun 3, 2016 at 15:33
MartinMartin
40.6k66 gold badges199 silver badges286 bronze badges
1 Answer
Reset to default 8In fact you will always load all the data using this. The skip
and take
operators apply only if you have several events received in the data flow:
- skip: skip a number of events
- take: take into account only a specified number of events
In your case (an HTTP request), you only have one: when you get the data. So if you want to filter you data, you need to use another map
operator. Something like that:
getCars(offset: number, count: number): Observable<Car[]> {
return this.http
.get('resources/data/cars.json')
.map(this.extractData)
.map(data => {
return data.slice(offset, offset + count); // <----
})
.catch(this.handleError);
}
本文标签: javascriptHow to use Skip and Take with RxJs ObservableStack Overflow
版权声明:本文标题:javascript - How to use Skip and Take with RxJs Observable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801691a2398261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论