Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular resource custom url

I'm trying to add a simple method on an angular resource, basically like this:

/api/resource/:id/archive

If I do it just like this:

angular.module('myApp')
.factory('api', function($resource) {
    var api = {
        messages: $resource('/api/messages/:id', {id: '@id'}
        , {
            archive: {
                method: 'PUT'
                , params: {id: '@id'}
                , url: '/api/messages/:id/archive'
            }
        }
    return api;
})
.controller('myCtrl', function(api) {
    // once I get messages and do stuff...
    $scope.archive = function(msg) {
        api.messages.archive({id: msg._id}, successHandler, failureHandler);
    }
});

It doesn't work, I get a simple PUT (*/api/messages/{id}*). I tried it with another param.

In factory:

....
archive: {
    method: 'PUT'
    , params: {id: '@id', action: '@action'}
    , url: '/api/messages/:id/:action'
....

In controller:

api.messages.archive({id: msg._id, action: 'archive'} ...)

I get the second param as a query param, instead as part of the url.

like image 872
Zlatko Avatar asked Nov 18 '13 00:11

Zlatko


1 Answers

Well, after I typed the question, I saw I'm not using the latest angular. Angular 1.2.0 has this working.

like image 119
Zlatko Avatar answered Sep 21 '22 05:09

Zlatko