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!

Tom O'Brien
  • 1,741
  • 9
  • 45
  • 73
  • 2
    Classic case of not using an object in `ng-model`. This is a golden rule in angular – charlietfl Jan 13 '16 at 19:42
  • @charlietfl what do you mean Charlietfl? Tell us more... – Tom O'Brien Jan 13 '16 at 20:14
  • 2
    http://stackoverflow.com/questions/17606936/angularjs-dot-in-ng-model – charlietfl Jan 13 '16 at 20:16
  • @charlietfl It seems this was not working because it was inside a uib-tab. When I take it out of the uib-tab it works. So is this ng-model getting messed up by some scope confusion you think? – Tom O'Brien Jan 13 '16 at 20:30
  • 1
    did you change it to object? Nested scopes are why you can't use primitives for `ng-model` – charlietfl Jan 13 '16 at 20:35
  • no - I just changed the position of the pagination within the html so it wouldn't get mixed up with the scope. When you say object, what is the syntax exactly? – Tom O'Brien Jan 13 '16 at 20:46
  • 1
    did you look at link above I provided? Watch the video linked in first answer – charlietfl Jan 13 '16 at 20:51
  • 1
    Actually @charlietfl the angular-ui docs use $scope.currentPage https://github.com/angular-ui/bootstrap/blob/master/src/pagination/docs/demo.html – Fergus May 05 '16 at 04:42

2 Answers2

29

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>
Kamlesh Kumar
  • 1,632
  • 2
  • 21
  • 31
Viktor
  • 4,218
  • 4
  • 32
  • 63
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> 
Barani r
  • 2,119
  • 1
  • 25
  • 24