Skip to content

Instantly share code, notes, and snippets.

@JustMaier
Last active April 22, 2016 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustMaier/4abe1adb56c79d33ca173f881799cbb1 to your computer and use it in GitHub Desktop.
Save JustMaier/4abe1adb56c79d33ca173f881799cbb1 to your computer and use it in GitHub Desktop.
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