Pagination raise NG0956
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
I have currently the same issue.
Bootstrap: 5.3.8 ngx-bootstrap: 21.2.0 Angular: 21.2.12
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
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
- Use
<pagination>bound to atotalItemsvalue 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
Actual behavior Every recompute of pages destroys and recreates the entire
Root cause pagination/fesm2022/ngx-bootstrap-pagination.mjs, PaginationComponent template:
@for (pg of pages; track pg) {
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).
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
- Add
<pagination>with[totalItems]="23"and[itemsPerPage]="6"(4 pages). - Change the inputs or the current page.
- 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 pgline) - Angular: 21.2.25
- Browser: Chrome