通过$ resource angualrjs获取条件的数据 [英] Get Data on conditions by $resource angualrjs
问题描述
vm.appointments = AppointmentsService.query();
,我的服务代码是
(function(){
'use strict';
angular
.module('约会')
.factory(' AppointmentsService),
AppointmentsService $ inject = ['$ resource'];
函数AppointmentsService($ resource){
return $ resource api /约会/:约会ID',{
约会:'@_id'
},{
更新:{
方法:'PUT'
}
});
}
})();
现在我可以在此代码块中给出条件 AppointmentsService.query({condition });
或更改节点休息API中的服务。
如果是,那么我的AppointmentsService.query调用将是什么?
对于您不同的URL路径,您可以创建新方法如下所示,或者您可以将startDate作为查询字符串传递
控制器:
For Path Param
vm.appointments = AppointmentsService.searchByDate({date:'03 / 30/2016'});
查询参数
vm.appointments = AppointmentsService.searchByDate({StartDate:'03 / 01/2016',EndDate:'03 / 30/2016'});
服务:
函数AppointmentsService($ resource){
return $ resource('api /约会/:约会',{
约会:'@_id'
},{
update:{
method:'PUT'
},
// For Path Param
searchByDate:{
method:'GET ',
url:'你的url /:date'
},
//查询参数
searchByDate:{
方法:'GET',
url:'你的url /:startDate /:endDate',
params:{startDate:'@StartDate',endDate:'@EndDate'}
}
});
}
I am using $resource service for my crud operations now i want to get data on a condition like get appointments whose starting date is today. I am fetching all data by
vm.appointments = AppointmentsService.query();
and my service code is
(function () {
'use strict';
angular
.module('appointments')
.factory('AppointmentsService', AppointmentsService);
AppointmentsService.$inject = ['$resource'];
function AppointmentsService($resource) {
return $resource('api/appointments/:appointmentId', {
appointmentId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
})();
Now can i give condition in this code blockAppointmentsService.query({condition});
or change my service in node rest API.
If yes, then what will be my AppointmentsService.query call
For your different url path, you can create new method like below or you can pass startDate as a query string
Controller :
For Path Param
vm.appointments = AppointmentsService.searchByDate({date:'03/30/2016'});
For Query Param
vm.appointments = AppointmentsService.searchByDate({StartDate:'03/01/2016',EndDate:'03/30/2016'});
Service:
function AppointmentsService($resource) {
return $resource('api/appointments/:appointmentId', {
appointmentId: '@_id'
}, {
update: {
method: 'PUT'
},
// For Path Param
searchByDate :{
method : 'GET',
url : 'your url/:date'
},
// For Query Param
searchByDate :{
method : 'GET',
url : 'your url/:startDate/:endDate' ,
params : { startDate : '@StartDate', endDate : '@EndDate' }
}
});
}
这篇关于通过$ resource angualrjs获取条件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!