简体   繁体   English

将Angular变量传递给指令

[英]Pass an Angular variable to the directive

I'm building a pagination in angular, here is my directive: 我正在按角度建立分页,这是我的指令:

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')
            };
        }]
    };
});

and here is the posts-genre-link.html I want to display: 这是我要显示的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>

It works great except for the href inside the ng-repeat . 除了ng-repeat内的href以外,它的效果都很好。 I would like that hopLink is the value of linkPrefix plus $index + 1 . 我希望hopLink是linkPrefix加上$index + 1的值。 How can I achieve this? 我该如何实现?

I've fixed this changing the hopLink to hopLink: linkPrefix, and inside the href I did: href="{{postsGenreLink.hopLink}}{{$index + 1}}" . 我已经解决了这个改变hopLinkhopLink: linkPrefix,并且内部href我所做的: href="{{postsGenreLink.hopLink}}{{$index + 1}}" This was way more simple than passing the $index value to the controller through a ng-click. 这比通过ng-click将$index值传递给控制器​​更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM