Angularjs 사용자 정의 명령 으로 페이지 플러그 인 구현(DEMO)

최근 프로젝트 는 angularjs 1.0 버 전 을 사 용 했 기 때문에 페이지 별로 데 이 터 를 조회 하 는 기능 과 관련 되 었 고 나중에 사용자 정의 명령 으로 이 기능 을 실현 했다.지금 은 자신 이 쓴 페이지 기능 을 공유 하기 위해 간단 하고 쉬 운 데 모 를 따로 만 들 었 다.주:이 인 스 턴 스 는 실제 인터페이스 데 이 터 를 호출 합 니 다.
우선,작은 demo 의 디 렉 터 리 구 조 는 다음 과 같 습 니 다.

코드 부분
다음은 모든 파일 의 코드 를 직접 붙 입 니 다.중점 은 ListCtrl.js 와 pageDirective.js 입 니 다.
1、index.html

<!DOCTYPE html>
<html lang="en" ng-app="app" ng-cloak>
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <!--<script src="lib/jquery.js"></script>-->
  <script src="lib/angular.js"></script>
  <script src="lib/angular-ui-router.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .left-menu {
      width: 200px;
      float: left;
      height: 100%;
    }
    .left-menu ul {
      list-style: none;
    }
    .left-menu ul li {
      height: 40px;
      line-height: 40px;
      background-color: #777;
      color: #ddd;
      margin-bottom: 5px;
      cursor: pointer;
    }
    .main {
      margin-left: 200px;
      padding: 10px;
    }
    .left-menu ul li.active {
      background-color: #333;
      color: #fff;
    }
  </style>
</head>
<body>
  <div style="width: 100%;height: 50px;background-color: #ddd;">    </div>
  <div class="left-menu" ng-controller="MenuCtrl">
    <ul>
      <li ng-click="goState('home')" ng-class="{active:curUrl == '/home'}">  </li>
      <li ng-click="goState('list')" ng-class="{active:curUrl == '/list'}">  </li>
    </ul>
  </div>
  <div class="main" ui-view="main"></div>
<script src="scripts/app.js"></script>
<script src="scripts/router.js"></script>
<script src="scripts/controllers/MenuCtrl.js"></script>
<script src="scripts/controllers/ListCtrl.js"></script>
<script src="scripts/controllers/HomeCtrl.js"></script>
<script src="scripts/directive/pageDirective.js"></script>
</body>
</html>
2.views/list.html(중요,목록 데이터 의 html 부분)

<style>
  table {
    border: 1px solid #000;
    border-collapse: collapse;
    width: 100%;
  }
  table th,table td {
    border: 1px solid #000;
    text-align: center;
    height: 30px;
  }
</style>
<table>
  <thead>
  <th>ID</th>
  <th>  </th>
  <th>    </th>
  </thead>
  <tbody>
  <tr ng-repeat="item in listData">
    <td>{{item.id}}</td>
    <td>{{item.nickname}}</td>
    <td>{{item.create_time}}</td>
  </tr>
  </tbody>
</table>
<div>
  <!--    -->
  <div page-directive page-config="pageConfig"></div>
</div>
3、views/home.html

<div>    </div>
<a ng-href="{{url}}" rel="external nofollow" target="_self">  </a>
4.views/directive/page-directive.html(중요,사용자 정의 명령 의 html 부분)

<style>
  .page {font-size: 14px;background-color: transparent;}
  .page .page-l select {width: 60px;height: 30px;}
  .page .page-r {float: right;padding-top: 10px;}
  .page .page-r ul {float: left;list-style: none;margin: 0;height: 30px;box-sizing: border-box;}
  .page .page-r ul li {float: left;list-style: none;height: 100%;line-height: 30px;border: 1px solid #ccc;border-right: 0 none;box-sizing: border-box;}
  .page .page-r ul li:hover {background-color: #e2e2e2;}
  .page .page-r ul li:last-child {border-right: 1px solid #ccc;}
  .page .page-r ul li a {text-decoration: none;display: block;height: 100%;padding:0 10px; color: #2A6496;}
  .page .page-r ul li a.active {background-color: #428BCA;color: #fff;}
  .page .page-r ul li span {display: block;height: 100%;padding:0 10px; color: #2A6496;cursor: pointer;}
  .page .page-r ul li span.ellipsis {cursor: default;}
</style>
<div class="page" style="width: 100%;height: 50px;line-height: 50px;">
  <div class="page-l" id="page_l" style="float: left;">
    <span>   <span id="total_count">{{pageConfig.totalCount}}</span>  </span>
    <div style="display: inline-block;margin-left: 20px;">
      <span>    </span>
      <select id="page_size">
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="50">50</option>
        <option value="100">100</option>
      </select> 
    </div>
  </div>
  <div class="page-r">
    <ul id="page_ul" class="page-ul"></ul>
  </div>
</div>
5、scripts/app.js

angular.module('app', ['ui.router', 'app.C', 'app.S', 'app.D'])
.constant('global',{  //      
  url:'http://www.baidu.com'
});
angular.module('app.C', []);
angular.module('app.S', []);
angular.module('app.D', []);
6.scripts/router.js(경로 설정)

var app = angular.module('app').config(['$stateProvider', '$urlRouterProvider',
  function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home'); //       ,      home
    $stateProvider
      .state('home',{
        url:'/home',
        views:{
          'main':{
            templateUrl:'views/home.html',
            controller:'HomeCtrl'
          }
        }
      })
      .state('list',{
        url:'/list',
        views:{
          'main':{
            templateUrl:'views/list.html',
            controller:'ListCtrl'
          }
        }
      })
  }
]).run(['$rootScope', '$state',
  function ($rootScope, $state) {
    $rootScope.$on('$stateChangeSuccess',
      function(event, toState, toParams, fromState, fromParams) {
        console.log($state.current.url);
        $rootScope.curUrl = $state.current.url;
      })
  }
])
7、scripts/controllers/MenuCtrl.js

angular.module('app.C').controller('MenuCtrl', ['$scope', '$state',
  function ($scope, $state) {
    $scope.goState = function (url,params) {
      if(!url) {
        $state.go('home')
      }else{
        if(params){
          $state.go(url,params)
        }else{
          $state.go(url)
        }
      }
    }
  }
])
8、scripts/controllers/HomeCtrl.js

angular.module('app.C').controller('HomeCtrl',['$scope', 'global',
  function ($scope, global) {
    $scope.url = global.url;  //    
  }
])
9、scripts/controllers/ListCtrl.js (    ,     controller  )

angular.module('app.C').controller('ListCtrl', ['$scope', '$http',
  function ($scope, $http) {
    //     
    //    (        )
    $scope.pageConfig = {
      // pageSize:10,  //    (    ,   10)
      pageIndex:1,  //    
      totalCount:0,  //    
      totalPage:0,   //   
      prevPage:'<    ',   //   (    ,   :<)
      nextPage:'    >',   //   (    ,   :>)
      firstPage:'<<   ',   //  (    ,   :<<)
      lastPage:'   >>',   //  (    ,   :>>)
      degeCount:3,       //               (    ,   3)
      isShowEllipsis:true  //             (true:  ,false:   )
    }
    $scope.listData = [];  //    
    //       (           )
    $scope.params = {
      page: $scope.pageConfig.pageIndex, //    
      pageSize: $scope.pageConfig.pageSize,  //    
      course_id: 537
    }
    $scope.getList = function () {
      var promise = $http({
        method:"post",
        url:"http://api.admin.app.mtedu.com/v1/census/course-student-learning-progress",
        params:$scope.params,
        headers:{Authorization:'Bearer newtask2017'},
      }).success(function(res){
        if(res.success){
          $scope.listData = res.data.list;
          $scope.pageConfig.totalCount = res.data.totalCount; //    
          $scope.pageConfig.totalPage = Math.ceil($scope.pageConfig.totalCount / $scope.pageConfig.pageSize); //   
          console.log('    :'+$scope.pageConfig.totalCount+';    :'+$scope.pageConfig.totalPage+';    :'+$scope.pageConfig.pageIndex);
          $scope.$broadcast("initPage")  //               
        }else{
          alert('    ');
        }
      }).error(function(data){
        alert('    ');
      })
    }
    $scope.getList()
    //              
    $scope.$on("clickPage", function(e, m) {
      $scope.params.page = $scope.pageConfig.pageIndex;
      $scope.params.pageSize = $scope.pageConfig.pageSize;
      console.log('pageSize='+$scope.params.pageSize);
      $scope.getList();
    })
  }
])
10.scripts/directive/pageDirective.js(중요 한 부분,사용자 정의 명령 의 js 부분)

angular.module('app.D').directive('pageDirective',['$rootScope',
  function ($rootScope) {
    var link = function (scope,elem,attr) {
      scope.pageConfig.pageIndex; //    
      scope.pageConfig.totalPage; //   
      scope.pageConfig.totalCount; //    
      scope.pageConfig.pageSize = scope.pageConfig.pageSize || 10; //    
      var prev = scope.pageConfig.prevPage || '<'; //     
      var next = scope.pageConfig.nextPage || '>'; //     
      var first = scope.pageConfig.firstPage || '<<'; //    
      var last = scope.pageConfig.lastPage || '>>'; //    
      var degeCount = scope.pageConfig.degeCount || 3; //           (  :3)
      var isShowEllipsis = scope.pageConfig.isShowEllipsis;  //             
      var ellipsisBtn = isShowEllipsis ? '<li><span class="ellipsis">...</span></li>' : '';
      //               
      scope.$on("initPage", function(e, m) {
        initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      });
      function initPage(totalPage, pageIndex, degeCount) {
        var pageHtml = '';
        var tmpHtmlPrev = '';
        var tmpHtmlNext = '';
        if(pageIndex - degeCount >= degeCount-1 && totalPage - pageIndex >= degeCount+1){  //     
          var count = degeCount; //             
          for(var i=0; i<count; i++){
            if(pageIndex != 1){
              tmpHtmlPrev += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+(pageIndex-(count-i))+'</a></li>';
            }
            tmpHtmlNext += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+((pageIndex-0)+i+1)+'</a></li>';
          }
          pageHtml = '<li><a id="first_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+first+'</a></li>'+
            '<li><a id="prev_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+prev+'</a></li>'+
            ellipsisBtn+
            tmpHtmlPrev +
            '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="active">'+pageIndex+'</a></li>'+
            tmpHtmlNext +
            ellipsisBtn+
            '<li><a id="next_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+next+'</a></li>'+
            '<li><a id="last_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+last+'</a></li>';
        }else if(pageIndex - degeCount >= degeCount-1 && totalPage - pageIndex < degeCount+1) { //   ,    
          var count = degeCount; //          
          var countNext = totalPage - pageIndex; //          
          if(pageIndex != 1){
            for(var i=0; i<count; i++){
              tmpHtmlPrev += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+(pageIndex-(count-i))+'</a></li>';
            }
          }
          for(var i=0; i<countNext; i++){
            tmpHtmlNext += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+((pageIndex-0)+i+1)+'</a></li>';
          }
          pageHtml = '<li><a id="first_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+first+'</a></li>'+
            '<li><a id="prev_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+prev+'</a></li>'+
            ellipsisBtn+
            tmpHtmlPrev +
            '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="active">'+pageIndex+'</a></li>'+
            tmpHtmlNext +
            '<li><a id="next_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+next+'</a></li>'+
            '<li><a id="last_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+last+'</a></li>';
        }else if(pageIndex - degeCount < degeCount-1 && totalPage - pageIndex >= degeCount+1){ //    ,   
          var countPrev = pageIndex - 1; //          
          var count = degeCount; //          
          if(pageIndex != 1){
            for(var i=0; i<countPrev; i++){
              tmpHtmlPrev += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+(pageIndex-(countPrev-i))+'</a></li>';
            }
          }
          for(var i=0; i<count; i++){
            tmpHtmlNext += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+((pageIndex-0)+i+1)+'</a></li>';
          }
          pageHtml = '<li><a id="first_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+first+'</a></li>'+
            '<li><a id="prev_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+prev+'</a></li>'+
            tmpHtmlPrev +
            '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="active">'+pageIndex+'</a></li>'+
            tmpHtmlNext +
            ellipsisBtn+
            '<li><a id="next_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+next+'</a></li>'+
            '<li><a id="last_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+last+'</a></li>';
        }else if(pageIndex - degeCount < degeCount-1 && totalPage - pageIndex < degeCount+1){  //      
          var countPrev = pageIndex - 1; //          
          var countNext = totalPage - pageIndex; //          
          if(pageIndex != 1){
            for(var i=0; i<countPrev; i++){
              tmpHtmlPrev += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+(pageIndex-(countPrev-i))+'</a></li>';
            }
          }
          for(var i=0; i<countNext; i++){
            tmpHtmlNext += '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="page-number">'+((pageIndex-0)+i+1)+'</a></li>';
          }
          pageHtml = '<li><a id="first_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+first+'</a></li>'+
            '<li><a id="prev_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+prev+'</a></li>'+
            tmpHtmlPrev +
            '<li><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="active">'+pageIndex+'</a></li>'+
            tmpHtmlNext +
            '<li><a id="next_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+next+'</a></li>'+
            '<li><a id="last_page" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >'+last+'</a></li>';
        }
        document.getElementById('page_ul').innerHTML = pageHtml;
      }
      /*    (  、   、   、  )*/
      document.getElementById('page_ul').addEventListener('click', function (e) {
        console.log(456);
        var _this = e.target;  //      a  
        var idAttr = _this.id; //id  
        var className = _this.className;  //class  
        if(idAttr == 'first_page'){ //        
          scope.pageConfig.pageIndex = 1;
        }else if(idAttr == 'prev_page'){  //         
          scope.pageConfig.pageIndex = scope.pageConfig.pageIndex == 1 ? scope.pageConfig.pageIndex : scope.pageConfig.pageIndex - 1 ;
        }else if(idAttr == 'next_page'){ //         
          scope.pageConfig.pageIndex = scope.pageConfig.pageIndex == scope.pageConfig.totalPage ? scope.pageConfig.pageIndex : parseInt(scope.pageConfig.pageIndex) + 1;
        }else if(idAttr == 'last_page'){ //        
          scope.pageConfig.pageIndex = scope.pageConfig.totalPage;
        }else if(className == 'page-number'){  //          
          scope.pageConfig.pageIndex = _this.innerText;
        }
        initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount);
        scope.$emit('clickPage');
      });
      /*      */
      document.getElementById('page_size').addEventListener('change', function () {
        var _this = this;
        scope.pageConfig.pageIndex = 1;
        scope.pageConfig.pageSize = _this.value - 0;
        initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount);
        scope.$emit('clickPage');
      })
      ///////////////////////////         jQuery         /////////////////////////////
      /*     */
      // $('#page_ul').on('click','a',function () {
      //   var _this = $(this);
      //   scope.pageConfig.pageIndex = _this.text();
      //   initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      //   scope.$emit('clickPage')
      // });
      /*    */
      // $('#page_ul').on('click','#first_page',function () {
      //   var _this = $(this);
      //   scope.pageConfig.pageIndex = 1;
      //   initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      //   scope.$emit('clickPage')
      // });
      // /*     */
      // $('#page_ul').on('click','#prev_page',function () {
      //   var _this = $(this);
      //   scope.pageConfig.pageIndex = scope.pageConfig.pageIndex == 1 ? scope.pageConfig.pageIndex : scope.pageConfig.pageIndex - 1 ;
      //   initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      //   scope.$emit('clickPage')
      // });
      // /*     */
      // $('#page_ul').on('click','#next_page',function () {
      //   var _this = $(this);
      //   scope.pageConfig.pageIndex = scope.pageConfig.pageIndex == scope.pageConfig.totalPage ? scope.pageConfig.pageIndex : parseInt(scope.pageConfig.pageIndex) + 1;
      //   initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      //   scope.$emit('clickPage')
      // });
      // /*    */
      // $('#page_ul').on('click','#last_page',function () {
      //   var _this = $(this);
      //   scope.pageConfig.pageIndex = scope.pageConfig.totalPage;
      //   initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      //   scope.$emit('clickPage')
      // });
      /*      */
      // $('.page-l').on('change', '#page_size',function () {
      //   var _this = $(this);
      //   scope.pageConfig.pageIndex = 1;
      //   scope.pageConfig.pageSize = _this.val()-0;
      //   initPage(scope.pageConfig.totalPage, scope.pageConfig.pageIndex, degeCount)
      //   scope.$emit('clickPage')
      // });
    };
    return {
      restrict: 'EA',
      'scope':{
        'pageConfig':'=',
      },
      templateUrl: 'views/directive/page-directive.html',
      link: link
    };
  }
])
효과





총결산
위 에서 말 한 것 은 소 편 이 소개 한 Angularjs 사용자 정의 명령 으로 페이지 플러그 인(DEMO)을 실현 하 는 것 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기