How to get an answer using ngResource


I have a controller with which I could ( finally ), save a record in my database, which is this:

  function PuestoAddController(puestosService, puesto){
    var self = this;
    var _puesto = puesto;

    function _add(_puesto){
      console.log("Puesto: " + _puesto.clave + " - " + _puesto.nombre + " - " + _puesto.orden);, function(){
        console.log('Registro Guardado');

    self.add = _add;

This works as expected, but I need to get the response from the server, for example, if it was successful, I get the code 201 , but it could be an error message of type Esta clave ya está registrada .

From the response received, I will schedule the actions that are necessary, for example, clean the form, send to another page, etc.

My service is this:

  'use strict';

    .factory('puestosService', PuestosService);

  PuestosService.$inject = ['$resource', 'config', 'session'];

  function PuestosService($resource, config, session){
    var puestosURL = config.baseURL + config.apiURL + '/capine/puesto/:id/';
    return $resource(
      {id: '@id'},
        'get': {cache: true, isArray: false},
        'query': {method:'GET', isArray: true, cache: true},
        'save': {
          method: 'POST',
          headers: {'Authorization': 'Token '+ session.getAccessToken()}

In the module documentation there is an action called transformResponse , but unfortunately not offer no example of use.


How can I get the response from the RESTful server if I use ngResource ( $resource ) in AngularJS 1.x?

Thanks for your time.

asked by toledano 24.11.2016 в 21:02

1 answer


Another way to do this is to use the $promise property, for example:$promise
answered by 24.11.2016 / 21:18