6

I have created a pager widget using template file which I am using twice in my HTML page. I have a select for go to page option and also links for previous and next page.

Problem is that when I use select box for updating current page its updated and then I use previous and next page links, the current page gets updated but select box doesn't get updated.

Please tell me what I am doing wrong. Is there any logical mistake in my approach to building a pager widget like this?

Controller Code:

 var gallery = angular.module('gallery', []);
    gallery.controller('ItemListCtrl', ['$scope', function($scope){
      /*  Pagination Code */
    $scope.currentPage = 1;
    $scope.itemsPerPage = 24;
    $scope.total = 100;
    $scope.range = function(min, max, step){
      step = step || 1;
      var input = [];
      for (var i = min; i <= max; i += step) input.push(i);
      return input;
    };
    $scope.prevPage = function (){
      if($scope.currentPage > 1){
        $scope.currentPage--;
      }
    };
    $scope.nextPage = function (){
      if($scope.currentPage < $scope.pageCount()){
        $scope.currentPage++;
      }
    };
    $scope.pageCount = function (){
      return Math.ceil($scope.total / $scope.itemsPerPage);
    };
    $scope.setPage = function (n){
      if(n >= 0 && n <= $scope.pageCount()){
        $scope.currentPage = parseInt(n, 10);
      }
    };
  }]);

Here is the plnkr URL for reproducing the issue.

http://plnkr.co/edit/9LUJnVzWAS9BauyORQn5?p=preview

1 Answer 1

12

The root cause is ng-include will create a separate scope for targeted element, so a quick fix to your code is adding $parent prefix to all scope objects.

<fieldset class="pager">
<div>Page {{$parent.currentPage}} of {{$parent.pageCount()}}</div>
<div>
<div>
<label>Go to page</label>
<select ng-model='$parent.currentPage' ng-change="$parent.setPage($parent.currentPage)">
<option ng-repeat="n in range(1,$parent.pageCount())" value="{{n}}" ng-selected="n === $parent.currentPage">{{n}}</option>
</select>
</div>
<div>
<a href ng-click="$parent.prevPage()">Previous Page</a>
&nbsp;|&nbsp; 
<a href ng-click="$parent.nextPage()">Next Page</a>
</div>
</div>
</fieldset>

Per Angular document Understanding Scopes, Scope inheritance won't work as you expected when you try 2-way data binding (i.e., form elements, ng-model) to a primitive. This issue with primitives can be easily avoided by following the "best practice" of always have a '.' in your ng-models

5
  • I have read that adding $parent is kind of a hack. If you can tell me what is the angular way of creating such a widget what would be really helpful. Feb 16, 2015 at 6:06
  • 4
    Angular way of dealing with scope inheritance is using . dot in your ng-models, like $scope.info = { currentPage: 1, itemsPerPage : 24, total : 100}, then in your view, access them as info.currentpage, info.itemsPerPage.
    – Rebornix
    Feb 16, 2015 at 6:18
  • +1 .. Thanks a ton .. I was working on this for almost 3 hours .. but could not figure it out. using $parent fixed all my issues.
    – Amarnath
    Nov 12, 2015 at 17:02
  • @Che $parent can be a workaround, but you'd better use a.b dot notation to solve this problem
    – Rebornix
    Nov 13, 2015 at 5:23
  • I ended up taking the ng-include out as I had too many elements in my include to go back and start managing them with dot or $parent...I was only using it to make the html easier to understand. You saved me on this one.
    – iTrout
    Feb 7, 2016 at 19:45

Your Answer

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

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