通过 $resource angularjs 获取条件数据
Get Data on conditions by $resource angualrjs
我正在为我的 crud 操作使用 $resource 服务,现在我想获取条件数据,例如获取开始日期为今天的约会。我正在通过
获取所有数据
vm.appointments = AppointmentsService.query();
我的服务代码是
(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'
}
});
}
})();
现在我可以在此代码块中给出条件AppointmentsService.query({condition});
或更改节点剩余中的服务API。
如果是,那么我的 AppointmentsService.query 电话会是什么
对于不同的 url 路径,您可以像下面这样创建新方法,也可以将 startDate 作为查询字符串传递
控制器:
对于路径参数
vm.appointments = AppointmentsService.searchByDate({date:'03/30/2016'});
对于查询参数
vm.appointments = AppointmentsService.searchByDate({StartDate:'03/01/2016',EndDate:'03/30/2016'});
服务:
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' }
}
});
}
更新您的服务代码...
(function () {
'use strict';
angular
.module('appointments')
.factory('AppointmentsService', AppointmentsService);
AppointmentsService.$inject = ['$resource'];
function AppointmentsService($resource) {
var service = {
get: $resource('api/appointments/:appointmentId',{
appointmentId: '@_id'
},{
method:'GET'
}),
update: $resource('api/appointments/:appointmentId',{
appointmentId: '@_id'
},{
method:'PUT'
}),
query:$resource('api/appointments',{
method:'GET',
isArray:true
})
queryByStartDate:$resource('api/appointments/:startDate',{
startDate: '@_startDate'
},{
method:'GET',
isArray:true
})
}
return service;
}
})();
并在控制器中调用queryByStartDate
var startDate = new Date(); //you can use $filter to format date
$scope.appointments = AppointmentsService.queryByStartDate({startDate:startDate});
我正在为我的 crud 操作使用 $resource 服务,现在我想获取条件数据,例如获取开始日期为今天的约会。我正在通过
获取所有数据vm.appointments = AppointmentsService.query();
我的服务代码是
(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'
}
});
}
})();
现在我可以在此代码块中给出条件AppointmentsService.query({condition});
或更改节点剩余中的服务API。
如果是,那么我的 AppointmentsService.query 电话会是什么
对于不同的 url 路径,您可以像下面这样创建新方法,也可以将 startDate 作为查询字符串传递
控制器:
对于路径参数
vm.appointments = AppointmentsService.searchByDate({date:'03/30/2016'});
对于查询参数
vm.appointments = AppointmentsService.searchByDate({StartDate:'03/01/2016',EndDate:'03/30/2016'});
服务:
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' }
}
});
}
更新您的服务代码...
(function () {
'use strict';
angular
.module('appointments')
.factory('AppointmentsService', AppointmentsService);
AppointmentsService.$inject = ['$resource'];
function AppointmentsService($resource) {
var service = {
get: $resource('api/appointments/:appointmentId',{
appointmentId: '@_id'
},{
method:'GET'
}),
update: $resource('api/appointments/:appointmentId',{
appointmentId: '@_id'
},{
method:'PUT'
}),
query:$resource('api/appointments',{
method:'GET',
isArray:true
})
queryByStartDate:$resource('api/appointments/:startDate',{
startDate: '@_startDate'
},{
method:'GET',
isArray:true
})
}
return service;
}
})();
并在控制器中调用queryByStartDate
var startDate = new Date(); //you can use $filter to format date
$scope.appointments = AppointmentsService.queryByStartDate({startDate:startDate});