AngularJS: $手表

AngularJS: $watch

我有这个 属性 : $scope.Users.length ,根据它,我的应用程序显示了页码。尽管 属性 发生了变化,但分页没有变化。我想添加 $scope.$watch 来处理它,我该怎么做?

分页属性:

 $scope.totalItems = $scope.Users.length;
        $scope.currentPage = 1;
    $scope.itemsPerPage = 3;
    $scope.maxSize = (($scope.Users.length / 3) + 1) ; //Number of pager buttons to show

html中的分页:

<div>
        <pagination total-items="totalItems" ng-model="currentPage" max-size="maxSize" class="pagination-md" boundary-links="true" rotate="false" num-pages="numPages" ng-click="pageChanged(user)" items-per-page="itemsPerPage"></pagination>
    </div>

方法是每次向页面添加新数据时 $watch $scope.totalitems。

$watch 函数(放在你的添加数据函数中):

$scope.$watch('totalItems', function(newValue, oldValue) {
        $scope.pageChange($scope.Users.length);
    });

将更改 $scope.totalitems 的函数,它负责页数:

$scope.pageChange = function(pageNo) {
        $scope.totalItems = pageNo;
    };