I have installed a package called ngx-pagination. But i have a problem since when you navigate away from the previous page and you click the back button, it redirects you to the first page instead of going back to the previous page? How can i make it return to the previous page if i click the back button in the browser? Here's my code below.
TS
config: any;
constructor() {
this.config = {
currentPage: 1,
itemsPerPage: 2
};
this.route.paramMap
.map(params => params.get('page'))
.subscribe(page => this.config.currentPage = page);
}
}
ngOnInit() {
this.getAllBooks();
}
pageChange(newPage: number) {
this.router.navigate(['/books'], { queryParams: { page: newPage } });
}
getAllBooks() {
this.subscription = this.booksService.getAll()
.subscribe(
(data:any) => {
this.books = data;
console.log(data);
},
error => {
console.log(error);
});
}
HTML
<tbody>
<tr *ngFor="let book of books | paginate: { itemsPerPage: config.itemsPerPage, currentPage: config.currentPage }">
<td>{{ book.SKU }}</td>
<td>{{ book.name }}</td>
<td>{{ book.description }}</td>
</tr>
</tbody>
</table>
<pagination-controls (pageChange)=“pageChange($event)” class="my-pagination"></pagination-controls>