Angular pagination, set first page via the controller

3.2k views Asked by At

I am using an angular pagination directive, and I'd like to do something that I hope should be fairly simple.

Rather than defaulting to page one, I'd like the option to set the page the user starts on. So ideally, something like this.

In the html

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

In my controller

$scope.currentPage = 3;

$scope.pageChanged = function() {
    alert($scope.currentPage + " is the current page.");
}

Every time, the alert message displays

"1 is the current page."

So this tells me a few things.. pageChange is getting called, presumably when $scope.currentPage changes from 3 to 1. To further test this, the pageChanged function is NOT fired when I set $scope.currentPage equal to 1 in the controller. I'm guessing that when the DOM element is initialized, it defaults to 1, which changes my previous value of 3.

I think that gets at the gist of the problem... How do I force this directive to use the value I want, rather than the default of 1? I could not find anything like this on the docs page unfortunately, and directives still confuse me somewhat.

1

There are 1 answers

1
Icycool On BEST ANSWER

Your observation is correct. To solve this you'll need to set $scope.currentPage only when your data (or total-item) has come in.

$scope.setCount = function(){
  $scope.resultCount = 100;
  $scope.currentPage = 3;
}

Fiddle