Make an optional parameter in angularjs resource
I want to make the parameter optional in angular js resource like below:
Current implementation:
function TrackResource($resource) {
return $resource(
'track/:type/:id',
{ type: 'info', id: '@id' },
{
'info': { method: 'GET', params: { type: 'info', id: '@id' } },
'live': { method: 'GET', params: { type: 'live', id: null } },
'lyrics': { method: 'GET', params: { type: 'lyrics', id: '@id' } },
'rand': { method: 'GET', params: { type: 'rand', id: null } },
'recent': { method: 'GET', params: { type: 'recent', id: null } },
'top': { method: 'GET', params: { type: 'top', id: '@days' } },
'log': { method: 'GET', params: { type: 'log', id: '@id' } },
}
);
}
So id can be null many times, so instead of passing null, I want to make id an optional parameter. It can be done?
+3
source to share
1 answer
The solution I found was to simply not send this parameter in the request.
Just send
TrackResource.info({})
If you don't want the second option, it will automatically become null in the request, make sure you don't mention this in the ResourceStructure:
function TrackResource($resource) {
return $resource(
'track/:type/:id',
{ type: 'info', id: '@id' },
{
'info': { method: 'GET', params: { type: 'info', id: '@id' } },
'live': { method: 'GET', params: { type: 'live' } },
'lyrics': { method: 'GET', params: { type: 'lyrics', id: '@id' } },
'rand': { method: 'GET', params: { type: 'rand' } },
'recent': { method: 'GET', params: { type: 'recent' } },
'top': { method: 'GET', params: { type: 'top', id: '@days' } },
'log': { method: 'GET', params: { type: 'log', id: '@id' } },
}
);
}
+2
source to share