0

We have this service using ng-resource:

var app = angular.module('salonesbelleza', []);
app.service("CentroBorrar_srv", function ($resource, UrlBaseApi_srv) {
return{

        return $resource('wa-centros/:id',
            {id:'@id', access_token:'@access_token'},
            { "post": { method: "DELETE",
                isArray: false,
                headers: {
                    'Accept': 'application/json; q=1.0, */*; q=0.1',
                    'Content-Type':'application/json'
                }
            }}
        );

}

});

In the controller We use this way

var CentroBorrar_data = CentroBorrar_srv.post({
            id:10,
            othervar1:'VALUE_1',
            othervar1:'VALUE_2',
            access_token:'MY TOKEN'
        });

With this ng-resource We delete an element with id=10 using some extra vars like acces_token

This work very well. And this is the URL generated by this ng-resource

wa-centros/10?access_token=MY_TOKEN&othervar1=VALUE_1&othervar2=VALUE_2

All vars are sent using GET in the URL But We want to send some vars using POST and others using GET. For example, We want to send othervar1 and othervar2 using POST.

Is there a way We can force in the ng-resource definition which var in the controller must be sent by POST and which must be sent using GET

I feel lost on this subject

Thank you very much in advance

Falinsito
  • 135
  • 1
  • 13

1 Answers1

0

Yes, it is possible to add parameters. You have to declare a custom action for this. There are two ways in which a custom action with a "non-GET" type can be created ( as taken from the angular documentation ) :

  1. non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
  2. non-GET instance actions: instance.$action([parameters], [success], [error])

As i see you are using the first, namely non-GET "class" action. Based on your example you should have something like this :

return $resource('rest-link/:id',
            {id:'@id', access_token:'@access_token'},
            { "yourCustomActionNameHere": { method: "POST",
                isArray: false,
                headers: {
                    'Accept': 'application/json; q=1.0, */*; q=0.1',
                    'Content-Type':'application/json'
                },
                params : {yourParam1 : val}
            }}
        );

Notice that params have been added to your resource action, namely yourParam1. As i mentioned before it is a non-GET "class" action, meaning that you pass the data object too. You should take that into consideration when calling your resource like so :

<YourResourceNameHere>.yourCustomActionNameHere({yourParam1: 12}, dataObject)

You should take care with parameters, namely with default parameters. In the case of a non-GET method like yours not providing value for a parameter paramName, means that the search will go on to the object properties you defined in your dataObject sent to the POST method, searching for a name-value pair where name is the same with your paramName.


You should have a look on instance methods, they are easier to use in my opinion and provide greater flexibility and readability. For example you could have a call like CentroBorrar.$save({param : val}) for a POST action, where CentroBorrar is an instance and will hold you data object. ( short example here ). Really hope this helps.

Stefan.B
  • 136
  • 1
  • 5
  • Thank you very much for all this info. Can't wait to put into practice and then let you know Sure It hepls a lot because there is a lot of new info I didn't know about ngresources – Falinsito Jul 25 '16 at 20:23
  • Still need solve something. In your example create a custom action and set method to POST. That way you can get variables by POST. But I need use method DELETE because that is the action need my server and my problem is with this action ngresource send all params by GET. But the back end need I send some params by Get and some mus go by POST in the same action. Is that possible? Can I especify in a resource action wich variabes must go by POST and wich must go by GET. For example Using a DELETE action. – Falinsito Jul 26 '16 at 04:47
  • I don't know any way in which that can be done. I'm not sure that is a possibility, doing a POST and a GET in the same action(different params), i tried to research the way in which you could write a custom action for this, i couldn't find anything really. You could try in your backend part ( e.g `spring`or whatever you are using ) doing the GET action first and then the DELETE on same transaction with the db. Maybe send some params on the DELETE action, then GET you data, afterwards continue with the `delete` action. If you find by any chance a solution in the near future please share it – Stefan.B Jul 26 '16 at 07:49
  • Thank you ever so much for your help. still fighting with this subject. I let you know if find a solution. – Falinsito Jul 27 '16 at 18:02
  • In short what is needed: We are sending query to the server using REST API and server side use YII PHP framework. This framework force us to name the action like DELETE or PUT or GET, etc and We need to pass some paremeter in the URL and some others by post simultaneously in the same DELETE or PUT or GET action call. How can We create a custom DELETE action request in ngResource so We pass some parameters by POST and some others in the URL itself ? – Falinsito Jul 27 '16 at 18:30
  • Take a look at this [answer](http://stackoverflow.com/questions/23609887/how-to-pass-body-payload-to-angular-resource-delete-call) – Falinsito Jul 27 '16 at 18:55
  • This [other link](http://stackoverflow.com/questions/19529483/how-do-i-not-send-url-template-parameters-with-request-body-in-angular) is about this subject too – Falinsito Jul 27 '16 at 19:26
  • Really nice info, i had no idea of ` transformRequest` option in a custom action. I did not find a way in which you can satisfy your needs, there is no way in which you can create a **DELETE** action, and 'pass some parameters by **POST** and some others in the URL itself'. I do not think this is possible being two completely different actions, and interpreted accordingly. If i find any type of workaround for your specific frameworks ( angular + YII PHP ) i'm going to post it here. – Stefan.B Jul 29 '16 at 08:14