2014-06-27 31 views
0

如下面的代碼所示,分頁(來自用於引導庫的Angular指令)控件的存在會在初始加載時將模型的值更改爲1,有沒有辦法解決?我添加了一個Plunker here分頁控制更改了模型的值,它必須與1

<!DOCTYPE html> 
<html> 

<head> 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"> 
</head> 

<body ng-app="testApp" ng-controller="testCtrl"> 
    <pagination ng-model="currentPage"></pagination>  
</body> 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.0/ui-bootstrap-tpls.min.js"></script> 

<script> 
var testApp = angular.module('testApp', ['ui.bootstrap']); 

var testCtrl = function($scope) { 
    $scope.currentPage = 4; 

    $scope.$watch('currentPage', function(){ 
     alert($scope.currentPage); 
    }); 
} 

testCtrl.$inject = ['$scope']; 
testApp.controller('testCtrl', testCtrl); 
</script> 

</html> 

感謝

+0

你可以發佈演示演示你的問題。根據文檔和這個plunker它的工作原理,因爲你期望設置當前頁面爲4:http://plnkr.co/edit/mBJMyohp1JQI3LBqRLkr?p=preview – lucuma

+0

上面提供的代碼是一個完整的演示,但我會複製它爲了方便 – JMK

回答

1

我相信這個問題是你是不是specifing的總頁數。嘗試將total-items="totalItems"添加到您的指令中,並且該指令默認爲每頁10個項目。如果您只有10個項目,它將只顯示1.您還需要設置每頁的項目數量。

<pagination ng-model="currentPage" total-items="totalItems" items-per-page="itemsPerPage"></pagination> 

var testCtrl = function($scope) { 
    $scope.currentPage = 4; 
    $scope.totalItems = 10; 
    $scope.itemPerPage =1; 

    $scope.$watch('currentPage', function(){ 
    alert($scope.currentPage); 
    }); 
} 

這裏是一個演示:http://plnkr.co/edit/mZsBhzpA5lax883C5ihy?p=preview

+0

仍然是一樣的傷心,我最初(在我的實際生產代碼中)把它拿出來用於示例,因爲它沒有區別 – JMK

+0

奇怪的是,對於幾乎是代碼意義的代碼,它的工作很好。如果我放棄總項目,它只顯示1. – lucuma

+1

在我的prod代碼中,我在搜索後動態加載總項目,所以它最初爲空,您的回答是正確的 – JMK

1

您需要設置總項屬性的指令分頁:

<pagination ng-model="currentPage" total-items="totalItems" items-per-page="itemsPerPage">  </pagination> 

那麼你需要設置相應的屬性您的控制器的範圍:

$scope.currentPage = 4; 
$scope.totalItems = 10; // For example. 
$scope.itemsPerPage = 2; 

你可以在文檔中找到所有的詳細信息for the pagination directive

相關問題