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

Pagination raise NG0956

Open fchiumeo opened this issue 7 months ago • 4 comments

Bug description: Pagination triggers an angular warning when changing pages. NG0956 (The configured tracking expression (track by identity) caused re-creation of the entire collection of size 5. This is an expensive operation requiring destruction and subsequent creation of DOM nodes, directives, components etc. Please review the "track expression" and make sure that it uniquely identifies items in a collection. Find more at https://v21.angular.dev/errors/NG0956)

Adding the .number would solve the problem https://github.com/valor-software/ngx-bootstrap/blob/development/src/pagination/pagination.component.html#L24

Versions of ngx-bootstrap, Angular, and Bootstrap:

ngx-bootstrap: 21.2.0

Angular: lastest

Bootstrap: lastest

fchiumeo avatar Apr 23 '26 19:04 fchiumeo

I have currently the same issue.

Bootstrap: 5.3.8 ngx-bootstrap: 21.2.0 Angular: 21.2.12

re-script avatar May 30 '26 06:05 re-script

Confirmed here with Angular 21.2.14 and ngx-bootstrap 21.2.0.

We see:

NG0956: The configured tracking expression (track by identity) caused re-creation of the entire collection of size 5

This occurs from the pagination component in development mode.

In the installed bundle, the relevant internal template is in: node_modules/ngx-bootstrap/pagination/fesm2022/ngx-bootstrap-pagination.mjs:523

That generated template includes:

@for (pg of pages; track pg)

@Gillardo

SmartieCodes avatar Jun 02 '26 13:06 SmartieCodes

Summary of problem with FIX suggestion

PaginationComponent's template repeats page-link items with @for (pg of pages; track pg). Tracking by the object reference itself is equivalent to Angular's default identity tracking. Since pages is rebuilt as a fresh array of new objects every time getPages() runs (on init, on totalItems/itemsPerPage change, and on page change), Angular can never match old items to new ones — it tears down and rebuilds every <li> in the pager on every update, even when the rendered result is identical.

In dev mode this triggers Angular's own diagnostic:

NG0956: The configured tracking expression (track by identity) caused re-creation of the entire collection of size 6. This is an expensive operation requiring destruction and subsequent creation of DOM nodes, directives, components etc. Please review the "track expression" and make sure that it uniquely identifies items in a collection.

Environment

  • ngx-bootstrap: 21.2.0 (confirmed still present in the latest published version, 21.2.2, as of 2026-08)
  • @angular/core: 21.2.14
  • Affected component: PaginationComponent (ngx-bootstrap/pagination)

Steps to reproduce

  1. Use <pagination> bound to a totalItems value that is set asynchronously (e.g. after an HTTP response), rather than provided synchronously at creation:
    <pagination [boundaryLinks]="true" [maxSize]="20" [(ngModel)]="currentPage"
        [itemsPerPage]="pageSize" [totalItems]="totalItemsFromAsyncSource"
        (pageChanged)="onPageChanged($event)">
    </pagination>
    

Run the app in dev mode (ng serve) with the console open. Load the page, or change totalItems / trigger a page change. Expected behavior Angular reuses existing

  • DOM nodes/component instances for page numbers that still exist across an update; no NG0956 warning.

    Actual behavior Every recompute of pages destroys and recreates the entire

  • collection, and Angular logs NG0956 in dev mode.

    Root cause pagination/fesm2022/ngx-bootstrap-pagination.mjs, PaginationComponent template:

    @for (pg of pages; track pg) {

  • ...
  • } Each page-link object already carries a stable, unique primitive key — pg.number (see getPages(), which returns { text, number: num, active } per page). The template just isn't using it.

    Suggested fix Track by that existing field instead of the object reference:

    • @for (pg of pages; track pg) {
    • @for (pg of pages; track pg.number) { That's the entire fix — one token, no behavior change, no new fields needed. I can attach a patch-package diff against 21.2.0 if that's useful for verification, but the change above is the complete content of it.

    Note for triage Since this check only runs when ngDevMode is true, a static demo/docs deployment (production build, fixed totalItems set once at creation) won't reproduce or surface it — that's likely why it hasn't been caught before. It reproduces reliably whenever totalItems is supplied asynchronously, which is the common real-world case (e.g. paginating server-fetched data).

    SuperSoldado avatar Aug 05 '26 12:08 SuperSoldado

    This still happens in 21.2.2 and 22.0.0

    Bug description

    The pagination component shows this warning in the browser console (development mode):

    NG0956: The configured tracking expression (track by identity) caused re-creation of the entire collection of size 4. This is an expensive operation requiring destruction and subsequent creation of DOM nodes, directives, components etc.

    The template uses:

    @for (pg of pages; track pg) {
    

    pages is built again with new objects every time it changes. Because track pg compares objects by identity, Angular thinks every page is new and creates all page buttons again.

    Suggested fix

    Track by the page number, which is unique and stable:

    @for (pg of pages; track pg.number) {
    

    This is a one-line change in the pagination template.

    How to reproduce

    1. Add <pagination> with [totalItems]="23" and [itemsPerPage]="6" (4 pages).
    2. Change the inputs or the current page.
    3. Open the browser console. The NG0956 warning is shown.

    Versions

    • ngx-bootstrap: 21.2.2 (I also checked 22.0.0, it has the same track pg line)
    • Angular: 21.2.25
    • Browser: Chrome

    NisanurBulut avatar Oct 09 '26 22:10 NisanurBulut