简体   繁体   English

如何编写分页的AngularJS指令?

[英]How do I write an AngularJS Directive for Pagination?

I am trying to write an AngularJS Directive that helps me with pagination. 我正在尝试编写一个可帮助我进行分页的AngularJS指令。 I want the user to be able to see up to 5 numbered links (1, 2, 3, 4, 5) when searching for something. 我希望用户在搜索内容时最多可以看到5个带编号的链接(1、2、3、4、5)。 If there are more than 5 pages of results they will be able to click an arrow > and see links 6 - 10 and so on. 如果结果多于5页,他们将能够单击箭头>并查看链接6-10,依此类推。

This is what I have so far, note that I am not sure how to complete the 'element.attr' line: 到目前为止,这是我所拥有的,请注意,我不确定如何完成“ element.attr”行:

var ReportsApp = angular.module('ReportsApp', []);

ReportsApp.directive('paginate', function () {
    return {
        restrict: 'A',
        transclude: true,
    scope: {
        page: '=page'
    },
    link: function (scope, element, attrs) {
        var page = scope.page;
        updatePage();

        function updatePage() {
            if (page <= 5) {
                for (page = 1; page <= 5; page++) {
                    element.attr('paginate')
                }
            }
        }
    }
}
});

The html is: 的HTML是:

  <div id="pages">
    <a href="#" paginate page="p"></a>
  </div>

Why not using a template and angular ng-repeat instead? 为什么不使用模板和有角度的ng-repeat代替呢? something like this: 像这样的东西:

template: '<div id="pages">
            <a href="#" paginate page="n" ng-repeat="n in pages" ></a>
        </div>'

here is a sample where this goes: 这是一个示例:

return {
        restrict: 'A',
        transclude: true,
    scope: {
        page: '=page'
    },
    template: '<div id="pages">
        <a href="#" paginate page="n" ng-repeat="n in pages" ></a>
    </div>'
    link: function (scope, element, attrs) {
    ...

I have implemented something like similar for my project. 我为我的项目实施了类似的操作。 Please check the below code. 请检查以下代码。 User this directive on any html element which is having vertical scrolling . 在任何具有垂直滚动的 html元素上使用此指令。

 (function(ng) { 'use strict'; ng.module('commonUtilsModule') /** This directive used to load records from backend when mouse is scrolling down */ .directive('fosWhenScrolled', function() { return function(scope, elm, attr) { var raw = elm[0]; elm.bind('scroll', function() { if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) { scope.$apply(attr.whenScrolled); } }); }; }); }(angular)); 
 <div class="col-xs-12" fos-when-scrolled="fetchResults()"></div> 

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

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