Angular resource custom url

11,584

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

Share:
11,584

Related videos on Youtube

Zlatko
Author by

Zlatko

Web Tech Expert

Updated on September 14, 2022

Comments

  • Zlatko
    Zlatko over 1 year

    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.