Angel S. Moreno Angel S. Moreno - 2 years ago 247
AngularJS Question

How do I override Angular $resource save?

I have created a service like so:

fbApp.factory('Post', ['$resource',
function ($resource) {
return $resource('posts/:postId', {}, {
query: {method: 'GET', params: {postId: 'index'}, isArray: true}

In my controller I am saving new posts like so:

var postData = angular.copy($scope.formData);
postData.published = $filter('date')($scope.formData.published, 'yyyy-MM-dd HH:mm:ss', 'UTC');
var post = new Post({Post:postData});

I want to change the service's save method so that instead of
new Post({Post:postData}).$save()
I can simply do
new Post(postData).$save()
I took a look at$resource and I believe the key is to override the
method or use
. Documentation on either is lacking.

Answer Source

The save method let you pass custom data to the request.

angular.module('myApp').factory('myFactory', function($resource){
   return $resource ('api/comments/');

angular.module('myApp').controller('myController', function($scope, myFactory){
   $scope.myData = 'some data';$scope.myData, function(){
      //do something useful.


app.config(function($httpProvider) {

$httpProvider.defaults.transformRequest = function(data){
    //do something here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download