2016-03-30 3 views
0

Я использую $ resource service для своих операций с crud теперь я хочу получить данные о состоянии, например, получить встречи, чья дата начала сегодня. Я все выборки данных по

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 вызов

+0

вы можете передать STARTDATE в виде строки запроса и обновления ваш код сервера соответственно. –

ответ

1

Для вашего другого пути URL-адреса, вы можете создать новый метод, как показано ниже, или вы можете передать STARTDATE в виде строки запроса

Контроллер:

Для Путь Param

vm.appointments = AppointmentsService.searchByDate({date:'03/30/2016'}); 

Для запроса Param

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' } 
     } 
    }); 
    } 
1

Обновить код службы ...

(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}); 
Смежные вопросы