2016-06-14 58 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