Pagination on Angular JS 2
In this example i am using the Promise, but should be very slimier for Observables.
This is my service look likes.
import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {
PaginatedResult,
IQuestion,
IServerResponse,
ServerResponse
} from './../models/interfaces';
import { HttpClient } from './../common/http.client';
@Injectable()
export class QuestionService {
private apiUrl = 'MyAPI/Questions';
private http;
constructor(private httpClient: HttpClient) {
this.http = httpClient;
}
getPaged(searchTerm?: string, page?: number, pageSize?: number): Promise<PaginatedResult<IQuestion[]>> {
return this.http.get(this.apiUrl + "?page=" + page + "&pageSize=" + pageSize + "&searchTerm=" + searchTerm)
.toPromise()
.then(response => response.json() as PaginatedResult<IQuestion[]>)
.catch(this.handleError);
}
private handleError(error: any) {
// show the error
return Promise.reject(new ServerResponse(error.status, error.statusText, error.ok, error._body));
}
}
