0

I'm trying to enable/disable a button on the basis of an expression, as in the code below.

But the button is not enabling again whenever the value of curPage variable changed.

How to handle this case in angularjs?

<button
    id="prevPage" 
    ng-click="prevPageBtnClicked($event)" 
    ng-disabled="{{curPage == 1}}">
        Previous
</button>


<button
    id="nextPage" 
    ng-click="nextPageBtnClicked($event)" 
    ng-disabled="{{curPage == totalPage}}">
        Next
</button>

Update 1: controller code

var myController = app.controller("mainCtrl", function($scope, $http) {

    $scope.logs = {};
    $scope.curPageLogs = {};
    $scope.itemPerPage = 15;
    $scope.curPage = 1;

    ...

    $scope.nextPageBtnClicked = function(event) {
        if ($scope.curPage < $scope.totalPage) {
            $scope.curPage += 1;
        }
        $scope.generateCurPageLogs();
    }



    $scope.prevPageBtnClicked = function(event) {
        if ($scope.curPage > 1) {
            $scope.curPage -= 1;
        }
        $scope.generateCurPageLogs();
    }
Don Bottstein
  • 1,640
  • 13
  • 17
Vivek Kumar
  • 4,822
  • 8
  • 51
  • 85

2 Answers2

5
  1. You should use angular expressions i.e., {{..}} inside html directives

eg.

<input value="{{sampleObj.attribute}}"/>
  1. You should NOT use angular expressions i.e., {{..}} inside angular directives.

eg.

<input ng-value="sampleObj.attribute"/>

in your code, change your

ng-disabled="{{curPage == totalPage}}">

to

ng-disabled="curPage == totalPage">
0

Remove the {{ }}. Conditional code in ng-if, ng-disabled etc does not require curly braces. Its recommended to use ===.

<button
    id="prevPage" 
    ng-click="prevPageBtnClicked($event)" 
    ng-disabled="curPage === 1">
        Previous
</button>


<button
    id="nextPage" 
    ng-click="nextPageBtnClicked($event)" 
    ng-disabled="curPage === totalPage">
        Next
</button>
A G
  • 21,087
  • 11
  • 87
  • 112