2016-06-14 93 views
1

我的下一個Web API:REST AngularJS @resource參數化的要求

GET  List<EventHistory> '/service/eventhistories' 
GET  EventHistory  '/service/eventhistories/{id}' 
DELETE EventHistory  '/service/eventhistories/{id}' 
PUT  EventHistory  '/service/eventhistories' 
POST EventHistory  '/service/eventhistories' 

使用的角度,我想用@resource從服務器獲取信息。

angularApp.factory('eventHistoryFactory', function ($resource) { 
    return $resource('/inner/service/eventhistories/:id',{id:'@id'}); 
}); 

但是使用這個聲明我沒有任何API來根據一些數據請求頁面。

var pageRequest = { 
    size: size, 
    page: page 
}; 

或發送更新eventHistory實體。

+0

我不完全確定我明白。您是否擔心分頁或您沒有後端分頁實施的事實? –

+0

我已經回來和分頁,但我不能同時發送到具有ID(inRow參數)和可分頁數據的後端請求。 我知道,它應該像 eventHistoryFactory.get(pageRequest,功能(數據){ controllerScope.eventHistorySingle =數據; }) 但如何我要補充ID請求,如果我更新的實體? – Sergii

+0

簡短的回答是 - 你不。角度爲你做。在下面檢查我的答案。 –

回答

2

基於OP的評論:

說你要更新一個單一的實體:

.controller('someCtrl', function($stateParams, eventHistoryFactory){ 
//For the sake of the demonstration - id comes from the state's params. 
var eventHistory = eventHistoryFactory.get({id: $stateParams.id}); 

eventHistory.$promise.then(function(){ 
    //Modify the entity when HTTP GET is complete 
    eventHistory.address = 'New York'; 

    //Post the entity 
    eventHistory.$save(); 

    //If you wish to use PUT instead of POST you should declare that 
    //in the class methods of $resource 
}); 



//Another example using query 
var entries = eventHistoryFactory.query({ 
    page: 0, 
    size: 20, 
    before: Date.now() 
}); 

//This is translated into GET /inner/service/eventhistories?page=0&size=20&before=111111111111 
//and should be interpreted correctly by your backend. 

entries.$promise.then(function(){ 
    //entries now contain 20 first event history with date earlier than now. 
    var specificEntry = entries[0]; 

    //Same deal - modify the entity when HTTP GET is complete 
    specificEntry.address = 'New York'; 

    //Post the entity 
    specificEntry.$save(); 
}); 
1

第一個答案似乎不錯,但我認爲這種方式更容易理解和簡單的begginers:

eventHistoryFactory.get(pageRequest, function (returnData) { 
     console.trace('request processed successfully: ' + returnData); 
     params.total(returnData.totalElements); 
     $defer.resolve(returnData.content); 
    }, function (error) { 
     console.log('request processed with error: ' + error); 
    }) 

以動態方式發出頁面請求,該對象應該在來自ngTable當前屬性(使用ngTable API)的請求之前構建。

請注意eventHistoryFactory。它沒有pageRequest對象的參數,但是它的工作原理是 - 魔術。通過在url中的GET請求,你可以看到:

?page=2&size=25