I'm new in Angular and I have problem with pass params from url to view on first load.
In my URL i have parameter page:
.../catalog?page=3
In component I have next code:
export class CatalogListComponent implements OnInit {
page;
constructor(private route: ActivatedRoute){
}
ngOnInit(){
this.route.queryParams.subscribe(params => {
this.page = +params['page'] || 1;
});
}
setPage(page: number) {
this.router.navigate(['catalog'], { queryParams: {...this.queryParams, page } });
}
}
In view I usengb-pagination :
<ngb-pagination class="pagination"
[collectionSize]="items.total"
[rotate]="true"
[(page)]="page"
[maxSize]="5"
[pageSize]="20"
(pageChange)="setPage(page)"
>
</ngb-pagination>
When I'm open/refresh link in browser .../catalog?page=3ngb-pagination always shows me page 1 instead of 3 , but next navigation works fine([page num in url and in pagination the same).
What I'm doing wrong ?
As answered before, you should refactor your method with the following code:
ngOnInit(){
this.route.queryParams.subscribe(params => {
this.page = params['page'];
});
}
And there is a little trick to print what you want in the console:
console.log('my value is: ', example);
<ngb-pagination [collectionSize]="total | async"
(pageChange)="pageChanged($event)"
[(page)]="page" aria-label="Pagination"></ngb-pagination>
you should use / pass the $event as argument instead of page
You should add *ngIf
directive as attribute to ngb-pagination
. The code should look like this
<ngb-pagination *ngIf="total" [collectionSize]="total"...></ngb-pagination>
It worked in my case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.