13

I am trying to use a UI Bootstrap Pagination directive in my project, but for some strange reason the ng-model used for updating the current page is not working. The pagination is showing up correctly with the correct number of tabs and everything, but when I click on a different number, the ng-model variable is not getting updated. For the life of me, I can't figure out why not.

The code I am using is taken from the examples on the UI Bootstrap homepage:

<uib-pagination total-items="totalItems" ng-model="currentPage"
  ng-change="pageChanged()"></uib-pagination>

The controller looks like this:

$scope.currentPage = 1;
$scope.totalItems = 160;

$scope.pageChanged = function() {
  console.log('Page changed to: ' + $scope.currentPage);
};

Here are the different required sources I am including:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://code.angularjs.org/1.4.8/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.0.3.js"></script>

The strangest thing is I have created a plnkr which replicates my code and it works! You can see here: http://plnkr.co/edit/4DoKnRACbKjLnMH5vGB6

Can anybody think of any ideas that might be causing this not to work? I'm scratching my head here, and can't think of anything obvious I am doing wrong...

All help much apprecatiated!

8
  • 2
    Classic case of not using an object in ng-model. This is a golden rule in angular Commented Jan 13, 2016 at 19:42
  • 2
    stackoverflow.com/questions/17606936/angularjs-dot-in-ng-model Commented Jan 13, 2016 at 20:16
  • 1
    did you change it to object? Nested scopes are why you can't use primitives for ng-model Commented Jan 13, 2016 at 20:35
  • 1
    did you look at link above I provided? Watch the video linked in first answer Commented Jan 13, 2016 at 20:51
  • 1
    Actually @charlietfl the angular-ui docs use $scope.currentPage github.com/angular-ui/bootstrap/blob/master/src/pagination/docs/… Commented May 5, 2016 at 4:42

2 Answers 2

28

I ran through the same problem and solved it by wrapping all Angular UI Bootstrap Pagination parameters into the object, like so:

JS

$scope.pagination = {
    currentPage: 1,
    maxSize: 21,
    totalItems: data.count
};

HTML

<uib-pagination
    total-items="pagination.totalItems" 
    ng-model="pagination.currentPage" 
    max-size="pagination.maxSize"
    boundary-link-numbers="true" 
    ng-change="pageChanged()"></uib-pagination>
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know precisely (or have a theory) why this happens sometimes with the binding?
Weird. After including those properties in an object, it works.
0

I also had same issue. Found that the bootstrap-tpls-0.10.0.js was causing this issue. Just replaced it with bootstrap-tpls-0.11.0.js to make it work.

Below is the library combination that works:

<script src="http://code.angularjs.org/1.4.8/angular.js"></script>  
<script src="http://code.angularjs.org/1.4.8/angular-resource.js"></script>  
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.