I'm creating an ng-repeat and using an ng-model changing the amount of elements that are visible, this works fine. Also I'm creating a pagination using ng-click and ng-class, and here's where I'm having the problem.
I'm defining some variables in a controller's scope, such as amount-limit, total-pages, current-page like this:
// definitions
$scope.clients;
$scope.clientsAmount;
// amount of visible clients
$scope.limitArray = [8,12,16];
$scope.clientsLimit = 8;
// function to get the amount of pages
function getPages(els, limit) {
// get the total pages considrering the amount of elements
$scope.totalPages = Math.ceil(els/limit);
$log.log( 'get - total => ', $scope.totalPages );
}
Then I have some functions to go to the next/previous page. At page load those work fine, but when I change clientsLimit amount, even though the controller's scope value is updated (using $watch I call the getPages function again using the new value of clientsLimit) but when I click in the buttons with the ng-click directive the values for pages are not changed. The param is to check if this is the last/first or next/previous buttons.
// NEXT BUTTON
$scope.nextBtn = function(param){
$log.log( 'value => ', value );
// check if this is not the last page and the param is true
// go to the next page
if( $scope.currentPage !== $scope.totalPages && param ) {
$scope.currentPage++;
} else if (!param) {
$scope.currentPage = $scope.totalPages;
}// conditional end
};// next button end
// PREVIOUS BUTTON
$scope.prevBtn = function(param){
// check if this is not the first page and the param is true
// go to the preivious page
if( $scope.currentPage !== 1 && param ) {
$scope.currentPage--;
} else if ( !param ) {
$scope.currentPage = 1;
}// conditional end
};// previous button end
This is the HTML
<li ng-class="firstPageCheck()">
<a href aria-label="First" ng-click="prevBtn(false)">
<span aria-hidden="true">First</span>
</a>
</li>
<li ng-class="firstPageCheck()">
<a href aria-label="Previous" ng-click="prevBtn(true)">
<span aria-hidden="true">Previous</span>
</a>
</li>
<li ng-class="lastPageCheck()">
<a href aria-label="Next" ng-click="nextBtn(true, value=totalPages)">
<span aria-hidden="true">Next</span>
</a>
</li>
<li ng-class="lastPageCheck()">
<a href aria-label="Last" ng-click="nextBtn(false, value=totalPages)">
<span aria-hidden="true">Last</span>
</a>
</li>
As I said the ng-class and ng-click are not reflecting the changes in the controller's scope.
Here's a plunkr with the files and the JSON data:
http://plnkr.co/edit/eVSW449vJ5cv6WWvyJhc?p=info
=======================================
EDIT
Hi. Basically this is the "I'm an idiot edit" :P. Turns out that I was adding more than one instance of the controller, therefore the select ng-model was updating just that controller instance, while the others (yes... at some point I included up to 3 :S) kept the value setted on run time.
Here's an updated Plunker link with just one controller instance and everything working ok with the pagination: http://plnkr.co/edit/eVSW449vJ5cv6WWvyJhc