After creating a custom service
to store all search parameters, I can easily access them from any part of my code. This ensures that the search parameters are always accurate. Below is the definition of the custom service
:
App.factory('filterService', function () {
var FilterService = {
actorId: '0001234',
actionStatus: ['BUILDING', 'STARTED', 'SENT'],
payer: null,
limit: 10,
offset: 0,
sortBy: null,
filterBy: null,
actionType: ['EB', 'AUTH'],
actorType: 'REQUESTING'
};
return FilterService;
});
When making a call to my REST service, I can utilize the search criteria stored in the custom service
as demonstrated below:
App.factory('encounterService', function ($resource, filterService) {
return {
resource: $resource('/internal/v2/encounters/:encounterId', {encounterId:'@encounterId'}, {
search: {
method: 'GET',
headers: {
'RemoteUser': 'jhornsby',
'Content-Type': 'application/json'
},
params: {
actorType: filterService.actorType,
actorId: filterService.actorId,
actionStatus: filterService.actionStatus,
actionType: filterService.actionType,
limit: filterService.limit,
offset: filterService.offset,
payer: filterService.payer,
filterBy: filterService.filterBy,
sortBy: filterService.sortBy
}
};
}
});
To execute the search
function, I make the following call:
encounterService.resource.search({}, function(data) {
// perform actions on data
});
However, when updating the filterService
values from a controller
, the changes do not reflect in the filterService
. An example of updating the filterService
is shown below:
$scope.ok = function() {
// Update the filter items using this.model
filterService.actorId = $scope.model.actorId;
filterService.actionStatus = $scope.model.actionStatus;
filterService.actionType = $scope.model.actionType;
filterService.limit = $scope.model.limit;
}
Whenever I call search
, it still uses the default values. How can I transform the search parameters into an object as intended?