Ben
Ben

Reputation: 23

RXJS and Angular: Take a number of elements in an array

I'm trying to wrap my head around RXJS.

I'm getting an array of Things via an http request.

But I'm having trouble taking a number of those objects.

Here's my method.

getThingsById(ID: number): Observable<IThing[]> {
return this._http.get(this.URL + ID + '/things)
  .map((response: Response) => <IThing[]>response.json())
  .take(20);
}

When I try to take 20 of those things I get all of them.

What am I doing wrong?

Thanks!

Upvotes: 1

Views: 2311

Answers (2)

Andriy
Andriy

Reputation: 15442

try Array.slice() method:

getThingsById(ID: number): Observable<IThing[]> {
  return this._http.get(`${this.URL}${ID}/things`)
    .map((response: Response) => <IThing[]>response.json().slice(0, 20));
}

Upvotes: 2

DeborahK
DeborahK

Reputation: 60528

Observables don't appear to work as described when using it with Http. With Http there is ONE response that comes back from the server. That ONE response has the entire set of requested data.

So the Observable contains the entire set of requested data from the ONE response.

Upvotes: 1

Related Questions