My code explained:
Below is a simple table with an ng-repeat
that loops through ctrl.persons
, and stores the results in the variable ctrl.results
. There is a filter included that allows a user to search the results using a text input, and sliceResults
is a custom filter for my Angular-UI pagination (it determines the offset and limit of results to display based on the current page of the pagination).
<table>
<tr ng-repeat="result in ctrl.results = (ctrl.persons | filter:searchKeywords) | sliceResults: ctrl.paginationOffset : ctrl.paginationLimit">
<td>{{ $index+1 }}</td>
<td>{{ result.firstName }}</td>
<td>{{ result.lastName }}</td>
</tr>
</table>
Results : {{ ctrl.results.length }}
Situation:
At the end of the code, ctrl.results.length
returns the amount of results real-time, and changes accordingly when a user fills out the text input to search through the results.
However, when the amount of results change, my pagination does not change accordingly and therefore shows an incorrect amount of pages (e.g. it can still show 3 pages while there is only 1 page). The amount of pages is based on a variable in my controller (this.paginationTotalItems
).
Question:
How do I update the variable this.paginationTotalItems
in my controller as soon as the amount of results (ctrl.results.length
) change in my view? (Or, what alternative way is there to update my pagination?)
What I tried:
I am far from being an AngularJS expert, so don't laugh at me! I tried to put a function in ng-change
with the result count, which failed because apparently it requires ng-model
and I don't see that working with ng-repeat
.
I also tried to use $watch
on the ctrl.results
variable, but that results in a massive console error because it aborts after x iterations. Also, I'd rather not use $watch
because I read it only works with $scope
, which I'd rather not use (I use the "controller as" syntax).
Thanks in advance, I'll accept the answer that helps me solve my issue.