html - 我怎样才能通过 AngularJS 进行分页?

标签 html angularjs http pagination

最近我创建了一个 AngularJs 应用程序。那是代码在下面

HTML:

      <div ng-app="" ng-controller="Hello">
            <ul>
                <li ng-repeat="x in greeting">
                    {{ x.user_name }}
                </li>
            </ul>
        </div>

我的 JS 代码是:

function Hello($scope, $http) {
$http.get('http://localhost/google/cibl/dashboard/get_all_user').
    success(function(data) {
        $scope.greeting = data;
    });

它工作正常。现在这个 http 服务给我 2000 行,我想用 AngularJs 对它进行分页。我该怎么做?

最佳答案

在你的 Controller 中

    app.controller('Hello', function($scope){
        $scope.pageSize = 10;
        $scope.currentPage = 0;

        $scope.changePage = function(page){
            $scope.currentPage = page;
        }
    })

在你的标记中,你应该有

    <div ng-app="" ng-controller="Hello">
        <ul>
            <li ng-repeat="x in greeting | startFrom: currentPage * pageSize | limitTo: pageSize">
                {{ x.user_name }}
            </li>
        </ul>
    </div>

我们缺少 startFrom 过滤器,所以让我们创建它

    app.filter('startFrom', function() {
        return function(input, start) {
            start = +start; //parse to int
            return input.slice(start);
        }
    });

现在剩下的就是分页面板了,我会留给你用 css 来美化它

    <ul class="pagination" >
        <li ng-repeat="page in pagination" ng-class="{'active':currentPage == page}"><a ng-click="changePage(page)">{{page + 1}}</a></li>
    </ul>

注意事项:

  1. 我们使用 changePage() 而不是 currentPage = page 的原因是因为 ng-repeat 可能会破坏一些变量
  2. 在您的 anchor () 标记中,您可以使用 href 来标记页面,而不是 ng-click,并在您的 Controller 中,观察页面 ref 并根据查询进行更改。这样做的好处是,当您决定为您的网站进行 SEO 时,它会为此做好准备!

    href="#!/partialname?page={{page}}"

关于html - 我怎样才能通过 AngularJS 进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914763/

相关文章:

php - 为什么我的 cookie 不工作 PHP

javascript - 如何将 Javascript 对象的内容复制到空对象中?

angularjs - 将本地 Node JS HTTP 服务器端口(在 Electron 中运行)传递给 angular.service

http - 当在 303 重定向上设置 cookie 时,浏览器不发送 cookie

php - 使用按钮删除 mysqli 数据库中的某些内容?

html - 如何在 CSS 中访问上一级目录?

angular - 在 Angular 6 应用程序上向下滚动时删除粘性类?

javascript - 以angularjs形式定义默认值,验证不起作用

vba - InternetOpenUrl 在第三次及后续调用时挂起并失败

java - Chrome 中的文件下载名称