通过$ resource angualrjs获取条件的数据 [英] Get Data on conditions by $resource angualrjs

查看:179
本文介绍了通过$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆