0

I have a simple directive for a table with pagination. The Pagination has a dropdown & a button. User can either select a page in the dropdown or click on the button to navigate. Say, the dropdown lists page numbers 1, 2, 3 and the selected page is 1. When user clicks on 'Next', the selected value in the dropdown should become 2.

Issue: When Next is clicked, though the scope variable SelectedPage is seen updated in the console, it is not reflecting in the view.

report.html:

<div>
<ul>       
   <li>
     <select ng-model="$scope.state.SelectedPage" ng-change="ShowPageResult()">
      <option ng-repeat="num in PageNumbers value="{{num}}">{{num}}</option>
     </select>
   </li>
   <li ng-click="ShowNextPage();"><a href=" #">Next</a></li>       
</ul>
<table>
...//some html
</table>
</div>

directive

app.directive('Report', function () {
    return {
        restrict: 'AE',            
        replace: 'true',
        templateUrl: '/views/report.html'            
    };
});

controller:

$scope.state={};
$scope.ShowPageResult = function () {
        GetReport($scope.state.SelectedPage);
    }
$scope.ShowNextPage = function () {
        $scope.state.SelectedPage = $scope.state.SelectedPage + 1;                
        GetReport($scope.state.SelectedPage);
    }

//get report data to bind to table
function GetReport(pageNumber) {
        $scope.UserReport = [];

        var promise = ReportsFactory.GetData();
        promise.then(function (success) {
            if (success.data != null && success.data != '') {                
                $scope.UserReport = success.data;                
                BindPageNumbers($scope.UserReport[0].TotalRows, pageNumber);              
            }            
        },
        function (error) {
            console.log(error);
        });
    }

  //bind page numbers to dropdown in pagination
    function BindPageNumbers(totalRows, selectedPage) {        
        $scope.PageNumbers = [];
        $scope.LastPageNumber = Math.ceil((totalRows / 10));
        for (var i = 1; i <= Math.ceil((totalRows / 10)) ; i++) {
            $scope.PageNumbers.push(i);
        }
        $scope.state.SelectedPage = selectedPage; //can see the no. becomes 2 here.            
    }
6
  • Where is the report directive being used? Commented Jul 8, 2015 at 7:20
  • In the aspx page <report></report> Commented Jul 8, 2015 at 7:22
  • There is no controller definition for the directive? It doesn't make sense how in the report directive template you're calling functions that don't belong to itself. Commented Jul 8, 2015 at 7:29
  • 1
    Have you tried using transclude: true in the directive? Since you have a scope parameter in your directive, a new scope is created for the directive while you want to access the parent's directive Commented Jul 8, 2015 at 7:30
  • @User234.. When I click on Next, I do get the data from database for the next page and it is displayed in the report. Only the page in the dropdown isnt updated. Commented Jul 8, 2015 at 7:33

2 Answers 2

1

My guess:

ng-change="ShowPageResult()" is declared in the directive template and because your directive declares its own scope, then it looks for the definition of ShowPageResult() in the directive's scope and not in the controller.

Therefore the change handler function is null and does nothing.

Sign up to request clarification or add additional context in comments.

3 Comments

I also have `ShowPageResult()' in the controller. Will that help
You have 2 different scopes. Either change the directive to not declare its own scope and use the parent's or change your SelectedPage value to be the one in the directive's scope
I have removed the scope from directive. Still doesnt work. Any more changes I have to do in the controller?
1

Try putting SelectedPage in an object. So in your controller

$scope.state = {
    SelectedPage: 1
};

You need to initialise the SelectedPage.

Then reference SelectedPage with state.SelectedPage in your controller and in your directive template:

<select ng-model="state.SelectedPage" ng-change="ShowPageResult()">

Because of javascript prototypical inheritance, the SelectedPage in your directive will be different to the one in your controller. See Understanding Scopes

And it's not clear to me why you're binding BindPageNumbers to your directive scope. Firstly, there is no controller in your directive so it never gets called there and there is no call in the template either. Second, BindPageNumbers is not bound to the parent scope so the directive wont get that function anyway.

7 Comments

There is no scope parameter in the directive now. I have updated my question after putting state. The error says state is not defined at BindPageNumbers()
You should be using $scope.state.SelectedPage in your controller
are you calling $scope.$apply somewhere?
Plz ignore the error thing. Fixed that. But issue remains about the dropdown value
See working plnkr You need to initialise the SelectedPage of the state and in the template, you do NOT reference $scope. Also, I'm assuming you have a typo in your ng-repeat code. Should be <option ng-repeat="num in PageNumbers" value="{{num}}">{{num}}</option>
|

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.