ngx-bootstrap icon indicating copy to clipboard operation
ngx-bootstrap copied to clipboard

Setting currentPage in Pagination gives "Expression has changed after it was checked" exception.

Open ksampolski opened this issue 10 years ago • 12 comments

Hello! At first sorry for my terrible english ;) I have problem with setting current page in Pagination component. When i set currentPage like: private currentPage: number = 4; In my component class i have an error:

ORIGINAL EXCEPTION: Expression has changed after it was checked. Previous value: '4'. Current value: '1'

Is any 'dirty' fix for doing this now?

ksampolski avatar Aug 19 '16 12:08 ksampolski

Import the following:

import { ChangeDetectorRef } from '@angular/core';

Then try using changeDetectionRef.detectChanges();

That fixed it for me.

Svrkota-S avatar Aug 22 '16 07:08 Svrkota-S

I will try but it smell like dirty workaround :(

ksampolski avatar Aug 22 '16 09:08 ksampolski

it is not a workaround or bug, you just tell to app that it should detect changes

dicbrus avatar Aug 23 '16 08:08 dicbrus

After using changeDetectionRef.detectChanges() pagination works.. but after page load any pagination tiles is checked (screen below)

noname

I set currentPage in component by SetPage method in ngOnInit like this. this.getPostCount().then((post_count) => { this.totalItems = post_count.post_count; this.changeDetectionRef.detectChanges() if(this.currentFirstPage != 1){ this.setPage(this.currentFirstPage); }; }); No errors in console.

ksampolski avatar Aug 24 '16 11:08 ksampolski

You need to manually set currentPage to 1, and then use two-way data binding and bind currentPage to ngModel of your pagination, like this: [(ngModel)]="currentPage". That way, first page will be checked when pagination is loaded.

Svrkota-S avatar Aug 24 '16 11:08 Svrkota-S

Yes, when i set currentPage to 1 everything works just fine. But sometimes i need load with another page checked (for example user access link like. http://localhost:3000/posts/3 ). Then i got blank pagination like in picture.

ksampolski avatar Aug 24 '16 12:08 ksampolski

I'm having this issue as well.

sdornan avatar Sep 08 '16 20:09 sdornan

Same here...when i try to change the "currentPage" via a method called by numPages event, i get that error.

Due to an issue in angular2 repo that is a "feature" that only happens when in development mode...

stefanholzapfel avatar Sep 18 '16 21:09 stefanholzapfel

Hi, so just to understand.... if this issue is closed does it mean its fixed?

Thanks!

aviramga avatar Dec 26 '16 12:12 aviramga

Code related to this issue was removed, plus my reproduce sample doesn't throw issue any more. So...

valorkin avatar Dec 26 '16 13:12 valorkin

ok thank, updating to 1.1.16-3 solved it (from 1.1.16)

On Mon, Dec 26, 2016 at 3:16 PM, Dmitriy Shekhovtsov < [email protected]> wrote:

Code related to this issue was removed, plus my reproduce sample doesn't throw issue any more. So...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/valor-software/ng2-bootstrap/issues/862#issuecomment-269210621, or mute the thread https://github.com/notifications/unsubscribe-auth/AEBDMh7bruJ06Vu8oS60ycQqzZPCHk_pks5rL75JgaJpZM4Joa7Y .

-- Aviram Gabay

aviramga avatar Dec 26 '16 13:12 aviramga

Getting this issue now with Angular 9 upgrade (9.0.0 -Didn't get it with our Angular8 version)

Current NgxBootstrap version: "ngx-bootstrap": "5.3.2"

Wrapping the event fired by the numPages @Output in setTimeout(() => {...}); fixes the error, but it's not ideal...

JAspeling avatar Feb 11 '20 13:02 JAspeling