2015-03-03 66 views
0

我有这个代码http://plnkr.co/edit/thSwsookMIY6LZMftVhY?p=preview。我只是试着把ui-bootstrap的分页模块放在一个标签中。我的问题是currentPage在范围中的变化,但不刷新显示的项目。分页不能在选项卡内的角度自举引导程序上工作

控制器List.js

controllers.controller('List', function ($scope) { 

    $scope.itemsPerPage = 5; 
    $scope.currentPage = 1; 
    $scope.totalAds = 0; 

    $scope.pageCount = function() { 
     return Math.ceil($scope.totalAds/$scope.itemsPerPage); 
    }; 

    $scope.tabs = [ 
     { "category": "G", "active": true, "ads": [{ "title": "Tit 1 G", "content": "Content ad 1 g" }, { "title": "Tit 2 G", "content": "Content ad 2 g" }, { "title": "Tit 3 G", "content": "Content ad 3 g" }, { "title": "Tit 4 G", "content": "Content ad 4 g" }, { "title": "Tit 5 G", "content": "Content ad 5 g" }, { "title": "Tit 6 G", "content": "Content ad 6 g" }, { "title": "Tit 7 G", "content": "Content ad 7 g" }, { "title": "Tit 8 G", "content": "Content ad 8 g" }, { "title": "Tit 9 G", "content": "Content ad 9 g" }, { "title": "Tit 10 G", "content": "Content ad 10 g" }, { "title": "Tit 11 G", "content": "Content ad 11 g" }, { "title": "Tit 12 G", "content": "Content ad 12 g" }, { "title": "Tit 13 G", "content": "Content ad 13 g" }, { "title": "Tit 14 G", "content": "Content ad 14 g" }, { "title": "Tit 15 G", "content": "Content ad 15 g" }, { "title": "Tit 16 G", "content": "Content ad 16 g" }, { "title": "Tit 17 G", "content": "Content ad 17 g" }] }, 
     { "category": "C", "active": false, "ads": [{ "title": "Tit 1 C", "content": "Content ad 1 c" }, { "title": "Tit 2 C", "content": "Content ad 2 c" }, { "title": "Tit 3 C", "content": "Content ad 3 c" }] }, 
     { "category": "T", "active": false, "ads": [{ "title": "Tit 1 T", "content": "Content ad 1 t" }, { "title": "Tit 2 T", "content": "Content ad 2 t" }, { "title": "Tit 3 T", "content": "Content ad 3 t" }, { "title": "Tit 4 T", "content": "Content ad 4 t" }, { "title": "Tit 5 T", "content": "Content ad 5 t" }, { "title": "Tit 6 T", "content": "Content ad 6 t" }, { "title": "Tit 5 T", "content": "Content ad 5 t" }, { "title": "Tit 7 T", "content": "Content ad 7 t" }, { "title": "Tit 8 T", "content": "Content ad 8 t" }, { "title": "Tit 9 T", "content": "Content ad 9 t" }, { "title": "Tit 10 T", "content": "Content ad 10 t" }, { "title": "Tit 11 T", "content": "Content ad 11 t" }, { "title": "Tit 12 T", "content": "Content ad 12 t" }, { "title": "Tit 13 T", "content": "Content ad 13 t" }, { "title": "Tit 14 T", "content": "Content ad 14 t" }, { "title": "Tit 15 T", "content": "Content ad 15 t" }, { "title": "Tit 16 T", "content": "Content ad 16 t" }, { "title": "Tit 17 T", "content": "Content ad 17 t" }, { "title": "Tit 18 T", "content": "Content ad 18 t" }, { "title": "Tit 19 T", "content": "Content ad 19 t" }] }, 
     { "category": "A", "active": false, "ads": [{ "title": "Tit 1 A", "content": "Content ad 1 a" }, { "title": "Tit 2 A", "content": "Content ad 2 a" }] } 
    ]; 

    $scope.getTotal = function() { 
     $scope.totalAds = 0; 
     angular.forEach($scope.tabs, function (tab) { 
      if (tab.active) { 
       $scope.totalAds = tab.ads.length; 
      } 
     }); 
    }; 

    $scope.selectedAds = function (category) { 
     var ads = []; 
     angular.forEach($scope.tabs, function (tab) { 
      if (tab.category == category) { 
       ads = tab.ads; 
      } 
     }); 
     return ads; 
    }; 

    $scope.pageChanged = function() { 
     console.log('Page changed to: ' + $scope.currentPage); 
    }; 

    $scope.category = function() { 
     return $scope.tabs.filter(function (tab) { 
      return tab.active; 
     })[0].category; 
    }; 

    $scope.$watch('category()', function() { 
     $scope.currentPage = 1; 
     $scope.getTotal(); 
     var end = $scope.currentPage + $scope.itemsPerPage; 
     $scope.filteredAds = $scope.selectedAds($scope.category()).slice(0, end); 
    }); 

    $scope.$watch('currentPage', function() { 
     var begin = (($scope.currentPage - 1) * $scope.itemsPerPage), 
     end = begin + $scope.itemsPerPage; 

     $scope.filteredAds = $scope.selectedAds($scope.category()).slice(begin, end); 
    }); 

    $scope.getTotal(); 

}); 

的index.html

<!DOCTYPE html> 
    <html ng-app="app"> 
     <head lang="es"> 
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
      <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <title>Adiminstración del tablón de anuncios</title> 
      <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script> 
      <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.1/ui-bootstrap-tpls.min.js"></script> 
      <script type="text/javascript" src="app.js"></script> 
      <script type="text/javascript" src="List.js"></script> 
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" /> 
     </head> 
     <body ng-controller="List"> 
       <tabset> 
        <tab ng-repeat="tab in tabs" heading="{{tab.category}}" active="tab.active"> 
         <ul> 
          <li ng-repeat="ad in filteredAds"> 
           <section> 
            <h3 ng-bind="ad.title"></h3> 
            <p ng-bind="ad.content"></p> 
           </section> 
          </li> 
         </ul> 
         <pagination total-items="totalAds" items-per-page="itemsPerPage" ng-model="currentPage"></pagination> 
         <p> 
          Total items: {{totalAds}}<br /> 
          Items per page: {{itemsPerPage}}<br /> 
          Current Page: {{currentPage}} 
         </p> 
        </tab> 
       </tabset> 
     </body> 
    </html> 

我已经试过了在这里(How do I tell ui-Bootstrap what content to paginate?)上发布了解决方案,但它并没有为我工作。在页面刷新值currentPage但不会触发$ watch。

回答

0

我遇到的同样的问题,并通过代码中的小改动得到解决。这是因为tabset使用自己的控制器,并且父分页控制器变成了tabset的控制器。

在您的控制器添加:

$scope.data = {}; 

在模板变化NG-模式:

<pagination ng-model='data.currentPage'></pagination> 
相关问题