Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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

share|improve this question

I looked at you code and I didn't find import angular-ui, because you using pagination. Check out: https://angular-ui.github.io/bootstrap/ and simple example:

 'http://plnkr.co/edit/xxjIBw71hDp9AHSrEhfp?p=preview
share|improve this answer
    
Thanks Daniel!!, I'll give that a try. Never really thought about going that route, since creating a custom filter and adding some functions using built-in directives didn't seem too hard, I never considered using other directives, since eventually I'll turn the pagination into a custom directive. – Rodrigo Mar 22 at 21:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.