0

I have a $resource called Livres I need to send to my API that wait a json typed like this :

{livres: {
  id: 1
  name: "bidule"
}}

This is the resource :

angular.module('TestApp').factory('Livres', [
  '$resource', function($resource) {
    return $resource('api/v1/livres/:id', {
      id: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

And the called method to save :

$scope.addLivres = function() { //create a new livres. Issues a POST to /api/livress
  $scope.livres.$save(function() {
    $state.go('adminLivres'); // on success go back to admin home i.e. adminLivres state.
  });
};

This only send a json like

  {id: 1
  name: "bidule"}

How can I make the json send to be encapsulated into a livres {} ?

6
  • don't you specify what goes into the resource though... so you could manipulate the input? Hang on, I don't think your factory is correct... check my answer to this on forming factories.. Commented Jun 8, 2015 at 11:32
  • Hi @CallumLinington, the factory is good. It works fine, but I have something to do with the params option arguments I guess. I still can't figure how to do this. Commented Jun 11, 2015 at 11:56
  • could you not do this: Livres.$save({ livres: { id : 1, name: 'bidule' } }) Commented Jun 11, 2015 at 12:12
  • I will give it a try, but I would appreciate not to have to specify all attributes :p Commented Jun 11, 2015 at 16:38
  • No It does not work this way. This send the post request with get infos : http://0.0.0.0:3000/api/v1/livres?livres={ id : 1, name: 'bidule' } Commented Jun 12, 2015 at 13:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.