Skip to content

Instantly share code, notes, and snippets.

@JustMaier
Last active Apr 22, 2016
Embed
What would you like to do?
Handle 201 in angular $resource - Get Id
// Config the $resourceProvider
app.config(["$resourceProvider",function ($resourceProvider) {
// extend the default actions
angular.extend($resourceProvider.defaults.actions,{
save : {
method : "POST",
interceptor: {
response: function(response){
if(response.status == 201){
// Get the id from the location - I'm assuming that the ID is the last part of the url.
var locationParts = response.headers("Location").split('/');
response.resource.id = locationParts[locationParts.length - 1];
// Alternatively you can return an Id header and just use that
// response.resource.id = response.headers("Id");
}
return response.resource;
}
}
}
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment