【发布时间】:2014-07-25 20:46:30
【问题描述】:
我正在尝试为页面上 2 个位置存在的分页创建自定义指令。我有 2 个数据表,我想使用相同的分页指令。这是我的指令
app.directive('pagination', function () {
return {
restrict: 'A',
template: '<div ng-if="!isLoading" class="backup-pagination">\
<input type="button" ng-disabled="currentPage == 0" ng-click="currentPage = currentPage - 1" value="Prev" />\
<span>{{ currentPage+1 }} / {{ numberOfPages(backupList) }}</span>\
<input type="button" ng-disabled="currentPage >= backupList.length/pageSize - 1" ng-click="currentPage = currentPage + 1" value="Next" />\
</div>',
scope : {
currentPage: '=',
backupList: '='
},
link: function(scope, elem, attrs){
}
}
});
这里是 DOM
这是表格的一个分页指令
<div ng-if="!isLoading" pagination backup-type="active" current-page="ActiveCurrentPage" backup-list="data.ActiveBackups"></div>
这是另一张桌子的另一张
<div ng-if="!isLoading" pagination backup-type="inactive" current-page="InactiveCurrentPage" backup-list="data.InactiveBackups"></div>
问题是我的分页指令似乎无法访问控制器的父范围。似乎存在于控制器$scope 上的numberOfPages() 没有被调用。与pageSize 相同。我必须做些什么才能使我的所有应用程序都可以使用这个 1 指令,使用不同的数据集?
HTML
<div id="active-backups" class="backup-table-wrap">
<h3>Active Backups</h3>
<table id="active-backups-table" class="backup-table">
<thead>
<tr>
<th>Customer</th>
<th>Last Archived</th>
<th>Archive Size (GB)</th>
<th>Mailboxes</th>
<th>Items</th>
<th></th>
</tr>
</thead>
<tbody ng-if="isLoading">
<tr load-spinner></tr>
</tbody>
<tbody ng-if="!isLoading">
<tr ng-repeat="active in data.ActiveBackups | paginate:ActiveCurrentPage*pageSize | limitTo:pageSize">
<td><a ui-sref="order.overview({ orderId: active.ServiceId })" ng-click="select(active)">{{ active.Customer }}</a></td>
<td>{{ active.LastArchived }}</td>
<td>{{ active.ArchiveSizeGB | number:2 }}</td>
<td>{{ active.NumMailboxes }}</td>
<td>{{ active.ArchivedItems }}</td>
<td><input type="button" class="backup-restore-btn" value="Restore" /></td>
</tr>
</tbody>
</table>
<div ng-if="!isLoading" pagination backup-type="active" current-page="ActiveCurrentPage" backup-list="data.ActiveBackups"></div>
</div>
<div id="inactive-backups" class="backup-table-wrap">
<h3>Inactive Backups</h3>
<table id="inactive-backups-table" class="backup-table">
<thead>
<tr>
<th>Customer</th>
<th>Last Archived</th>
<th>Archive Size (GB)</th>
<th>Mailboxes</th>
<th>Items</th>
<th></th>
</tr>
</thead>
<tbody ng-if="isLoading">
<tr load-spinner></tr>
</tbody>
<tbody ng-if="!isLoading">
<tr ng-repeat="active in data.InactiveBackups | paginate:InactiveCurrentPage*pageSize | limitTo:pageSize">
<td><a href="#">{{ active.Customer }}</a></td>
<td>{{ active.LastArchived }}</td>
<td>{{ active.ArchiveSizeGB | number:2 }}</td>
<td>{{ active.NumMailboxes }}</td>
<td>{{ active.ArchivedItems }}</td>
<td><input type="button" class="backup-restore-btn" value="Restore" /></td>
</tr>
</tbody>
</table>
<div ng-if="!isLoading" pagination backup-type="inactive" current-page="InactiveCurrentPage" backup-list="data.InactiveBackups"></div>
</div>
【问题讨论】:
-
您已经使用指令创建了一个新范围。如果要使用父范围,则必须传入函数或使用 $parent.numberOfPages(backupList)
标签: angularjs pagination