ng-disable不会随着表达式中使用的变量的变化而更新

Viv*_*mar 0 angularjs

我正在尝试根据表达式启用/禁用按钮,如下面的代码所示。

但是,只要curPage变量值更改,该按钮就不会再次启用。

如何在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>
Run Code Online (Sandbox Code Playgroud)

更新1:控制器代码

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();
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

  1. 您应该使用角度表达式,即{{..}}在html指令内部

例如。

<input value="{{sampleObj.attribute}}"/>
Run Code Online (Sandbox Code Playgroud)
  1. 您不应该使用角度表达式,即{{..}}内部角度指令。

例如。

<input ng-value="sampleObj.attribute"/>
Run Code Online (Sandbox Code Playgroud)

在您的代码中,更改您的

ng-disabled="{{curPage == totalPage}}">
Run Code Online (Sandbox Code Playgroud)

ng-disabled="curPage == totalPage">
Run Code Online (Sandbox Code Playgroud)