2016-01-29 59 views
0

我收到錯誤消息「TypeError:嘗試使用Bootstrap分頁時無法讀取屬性」切片「未定義」。我相信這個錯誤發生在範圍問題上,同時試圖在ng-repeat中的'items'上應用.slice()方法。我已經包含了我認爲問題正在發生的代碼。我一直在使用這個職位AngularJS pagination and Cannot read property 'slice' of undefined作爲解決我遇到的問題的指南。AngularJS分頁類型錯誤:無法讀取未定義的屬性「切片」

Controllers.js:

defaultPage = angular.module('DefaultPage', ['ui.bootstrap']); 

defaultPage.controller('DefaultController', ['$scope', '$http', function ($scope, $http) { 
    $scope.data = {}; 
    $scope.totalItems = $scope.ctrl.info.length; 
      //console.log($scope.ctrl.info.length) 
      $scope.itemsPerPage = 25; 
      $scope.currentPage = 1; 
      $scope.max = "3"; 

      $scope.pageCount = function() { 
       var begin = (($scope.currentPage - 1) * $scope.itemsPerPage); 
       //console.log(begin) 
       var end = begin + $scope.itemsPerPage; 
       //console.log(end) 

       $scope.totalItems = $scope.items.slice(begin, end); 
       console.log($scope.items) 

      }; 
}]); 

HTML:

<div class="main" ng-controller="DefaultController as ctrl"> 
    <div class="search"> 
     <h1 class="shadow"></h1> 
     <div class="searchbox"> 
      <i class="fa fa-search"></i> 

      <input ng-model="query" placeholder="Search Issue Title or Case #" autofocus> 
     </div> 
    </div> 

    <ul class="channellist"> 
     <li ng-repeat="items in ctrl.info | filter: query | filter: issue" class="channel cf"> 

      <a ng-href="#/issues/{{ctrl.info.indexOf(items)}}"> 
       <img ng-src="{{items.user.avatar_url}}" alt="channel logo for {{items.user.avatar_url}}" /> 

       <div class="info"> 

        <h2 class="userName">{{items.user.login}}</h2> 
        <h4 class="case">case: #{{items.number}}</h4> 
        <h4 class="case">issue status: {{items.state}}</h4> 
        <div class="main"> 
         <h3 class="title">{{items.title}}</h3> 
         <p class="body">{{items.body}}</p> 
        </div> 
        <div class="label_info"> 
         <ul> 
          <li class="mark">Created:</li> 
          <li class="mark"> {{items.created_at}} 
          </li> 
          <li class="mark">Updated:</li> 
          <li class="mark"> {{items.updated_at}}</li> 
         </ul> 
        </div> 
       </div> 
      </a> 
     </li> 
     <uib-pagination boundary-links="true" max-size="max" items-per-page="itemsPerPage" total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()"></uib-pagination> 
    </ul> 
</div> 
+0

下面的鏈接解決了我的問題http://stackoverflow.com/questions/25723455/custom-filter-giving-cannot-read-property-slice-of-undefined-in-angularjs –

回答

0

你不能切東西不存在,嘗試先定義項目。

+0

我想切'項目'從ng-repeat在我的HTML。我怎樣才能做到這一點? user3574939

+0

你可以對重複做'ng-if',並與'$ index'比較,或者你可以實現一個過濾器,該過濾器接受一個開始和結束參數並進行比較這樣我 –

+0

@ paul147我有點新手Angular,我已經創建了過濾器,但我沒有使用ng-if。我已經研究了一個過濾器,但我不確定如何將它與UI結合-bootstrap paginate腳本我在controller.js中 – user3574939

相關問題