我正在建立一个有角度的分页,这是我的指示:
app.directive('postsGenreLink', function() {
return {
restrict: 'EA',
templateUrl: myLocalized.partials + 'posts-genre-link.html',
controller: ['$scope', '$element', '$routeParams', function($scope, $element, $routeParams) {
var currentGenrePage = (!$routeParams.page) ? 1 : parseInt($routeParams.page),
linkPrefix = (!$routeParams.slug) ? 'page/' : 'genre/' + $routeParams.slug + '/page/';
$scope.postsGenreLink = {
prevLink: linkPrefix + (currentGenrePage - 1),
hopLink: linkPrefix + (currentGenrePage),
nextLink: linkPrefix + (currentGenrePage + 1),
sep: (!$element.attr('sep')) ? '|' : $element.attr('sep'),
prevLabel: (!$element.attr('prev-label')) ? 'Previous Page' : $element.attr('prev-label'),
nextLabel: (!$element.attr('next-label')) ? 'Next Page' : $element.attr('next-label'),
hopLabel: (!$element.attr('Jump')) ? 'Jump' : $element.attr('Jump')
};
}]
};
});
这里是我要显示的posts-genre-link.html:
<nav aria-label="...">
<ul class="pagination pt-3">
<li class="page-item" ng-class="{'disabled' : currentGenrePage == 1}">
<a class="page-link" href="{{postsGenreLink.prevLink}}">Previous</a>
</li>
<li ng-repeat="i in [].constructor(totalPages) track by $index" class="page-item" ng-class="{'active' : currentGenrePage == $index + 1}"><a class="page-link" href="{{postsGenreLink.hopLink}}">{{$index + 1}}</a></li>
<li class="page-item" ng-class="{'disabled' : currentGenrePage >= totalPages}">
<a class="page-link" href="{{postsGenreLink.nextLink}}">Next</a>
</li>
</ul>
</nav>
除ng-repeat
内的href外,它的效果很好。我希望hopLink是linkPrefix
加$index + 1
的值。我怎样才能做到这一点?
答案 0 :(得分:0)
我已经解决了这个问题,将hopLink
更改为hopLink: linkPrefix,
并将href
改为href="{{postsGenreLink.hopLink}}{{$index + 1}}"
。
这比通过ng-click将$index
值传递给控制器更简单。