尋呼

2015-01-26 82 views
4

我使用尋呼以下自定義指令和它的偉大工程: https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination尋呼

但現在我有50餘項,並想使用服務器端分頁。我的後端是Azure移動Web服務,但我堅持嘗試獲取正確的REST調用,以及如何在Angular中使用它以便與我的自定義指令進行分頁。

我試圖通過我的服務,以充分利用這一如下:

// All Articles 
pfcServices.factory('pfcArticles', ['$resource', function ($resource) { 
return $resource('https://myrestcall.net/tables/articles/:articleID?$top=100&$inlinecount=allpages', { articleID: '@id' }, 
{ 
    'query': { method: "GET", isArray: false }, 
    'update': { method: 'PATCH' } 
} 
); 
}]); 

我的控制器(請注意,我刪除了投票功能,以保持這個貼子簡潔):

// Articles for Home Paging Test 
pfcControllers.controller('pfcHomeArticlesCtrlTest', ['$scope', 'auth', 'pfcArticles', function ($scope, auth, pfcArticles) { 

$scope.articles = []; 
$scope.totalArticles = 0; 
$scope.articlesPerPage = 50; // this should match however many results your API puts on one page 
getResultsPage(1); 

$scope.pagination = { 
    current: 1 
}; 

$scope.pageChanged = function (newPage) { 
    getResultsPage(newPage); 
}; 

function getResultsPage(pageNumber) { 
    // Using inlinecount creates two objects of "results" and "count" 
    pfcArticles.query(function(data) { 
     $scope.articles = data.results; 
     $scope.totalArticles = data.count; 
    }); 
}  
}]); 

我的HTML (注意,我從控制器中刪除了投票功能,使其更加簡潔):

<div ng-controller="pfcHomeArticlesCtrlTest"> 
     <table class="table table-striped"> 
      <tr> 
       <th>Votes</th> 
       <th>Title</th> 
       <th>Category ID</th> 
       <th>Link</th> 
      </tr> 

      <tr dir-paginate="article in articles | itemsPerPage:10 total-items=" totalusers"> 

       <td> 
        <div class="col-md-1 voting well"> 
         <div class="votingButton" ng-click="upVote(articlevote);"> 
          <i class="glyphicon glyphicon-chevron-up"></i> 
         </div> 
         <div class="badge badge-inverse"> 
          <div>{{article.articlevotes}}</div> 
         </div> 
         <div class="votingButton" ng-click="downVote(articlevote);"> 
          <i class="glyphicon glyphicon-chevron-down"></i> 
         </div> 
        </div> 
       </td> 
       <td>{{article.articletitle}}</td> 
       <td>{{article.articlecategoryid}}</td> 
       <td><a ng-href="#article/{{article.id}}/{{article.articlelink}}">{{article.articletitle}}</a></td> 
      </tr> 
     </table> 
     <dir-pagination-controls on-page-change="pageChanged(newPageNumber)"></dir-pagination-controls> 
    </div> 

我收到錯誤如下:

angular.min.js:101 Error: [$parse:syntax] http://errors.angularjs.org/1.3.4/ $parse/syntax? p0=total&p1=is%20an%20unexpected%20token&p2=33&p3=articles%20%7CNaNtemsPerPage%3A10%20total-items%3Dtotalusers&p4=total-items%3Dtotalusers at Error (native)

這似乎是引用「totalusers」但我的控制器有「totalArticles」

自定義指令有使用$服務器端分頁的例子HTTP ,但我正在使用ng-resource(上圖)。

.controller('UsersController', function($scope, $http) { 
$scope.users = []; 
$scope.totalUsers = 0; 
$scope.usersPerPage = 25; // this should match however many results your API puts on one page 
getResultsPage(1); 

$scope.pagination = { 
current: 1 
}; 

$scope.pageChanged = function(newPage) { 
getResultsPage(newPage); 
}; 

function getResultsPage(pageNumber) { 
// this is just an example, in reality this stuff should be in a service 
$http.get('path/to/api/users?page=' + pageNumber) 
    .then(function(result) { 
     $scope.users = result.data.Items; 
     $scope.totalUsers = result.data.Count 
    }); 
} 
}) 

什麼是分頁正確的REST URL格式,以及如何使用它我的角度應用程序內?我相信我可以使用$ top,$ skip和$ inlinecount,但我不確定每個自定義指令是如何轉換爲「頁面」的。這是什麼導致解析錯誤?

Ex。 ? https://myrestcall.net/tables/articles $頂部= 100 & $跳過= 50 & $ inlinecount =所有頁

+0

在添加更多的記錄,看來最大爲50每頁,但現在我怎麼編碼這個正確的頁面? – Kode 2015-01-26 23:01:46

+1

似乎是文章過濾器上的語法錯誤:dir-paginate =「article in articles | itemsPerPage:10 total-items =」totalusers「 – 2015-01-30 18:08:53

+0

這正是它的一部分,並對我發佈的答案做了一些調整。 – Kode 2015-01-31 04:23:17

回答