I find myself in an interesting situation where I have a piece of code that is functioning within my Angular 2 application - it's generating the correct value, but the method behind its success is unclear to me.
Specifically, I am using ng2-pagination for pagination management in my app. By inputting the desired number of results per page and calculating this against the array length of results, the pagination accurately determines the number of pages required.
In my scenario, the correct number of pages is being generated, indicating that ng2-pagination is effectively performing the necessary calculations. However, I'm uncertain about how it's currently accessing the array's length.
The relevant code snippet within my component involves calling a service and executing two functions during the OnInit cycle. The first function retrieves all records, while the second obtains the total count of these records:
ngOnInit() {
this.customerService.getAll()
.subscribe(resRecordsData => this.records = resRecordsData,
responseRecordsError => this.errorMsg = responseRecordsError);
this.customerService.getCustomerCount()
.subscribe(resRecordsData => this.customerCount = resRecordsData,
responseRecordsError => this.errorMsg = responseRecordsError);
}
Subsequently, in my view, I iterate through these items and apply the paginate pipe as follows:
<tr *ngFor="let record of records | paginate: { id: 'customers', itemsPerPage: 15, currentPage: page }">
Additionally, I configure some parameters for the paginate-pipe directly within the view:
<div *ngIf="!!records && records.length" class="pagination">
<pagination-controls class="paginator" (pageChange)="page = $event" id="customers"
maxSize="15"
directionLinks="true"
autoHide="true">
</pagination-controls>
</div>
Currently, my collection consists of just under 900 records. Despite not explicitly passing this information anywhere in the code, the correct pagination is displayed with exactly 60 pages. This poses the question of how the pipe is aware of the 900 items.