angularjs $resource not replacing variable in url

2019-05-31 10:34发布

Using AngularJS 1.2.16 and angular-resource 1.2.16.

I have a resource like:

$resource('api/:variable/path',
    {
        variable:'@variableName'
    });

When I do a get using something like

resourceIns.get({variable:'taco'});

The resulting ajax call replaces :variable properly and i get

api/taco/path

If I do a post like

resourceIns.save({variable:'taco'});

the resulting ajax call looks like

api/path

and 'taco' gets put in the POST body...

I've had trouble finding others complaining about this so, maybe this is what's supposed to happen?

edit: I just discovered that get uses 'variable' and save/POST uses 'variableName' in the above example. Anybody have an explanation for that?

Here's a fiddle showing the situation: fiddle

1条回答
乱世女痞
2楼-- · 2019-05-31 11:04

I ran across the same issue or one that presented itself in the same fashion. My resource, too, was not respecting parameters passed in through a .post method.

I was able to get it to work by directly passing in the expected parameters.

Using a $resource:

angular.module('myApp')
  .factory('ModuleProductProducts', function ($resource) {
    return $resource('/module-api/product-products/:siteId/:id/:controller', {
      id: '@id'
    },
    {
        'updateMedia': {
            method: 'POST',
            url: 'module-api/product-products/:id/media/:mediaId',

            // *** Here ***
            params: {
                id: '@id',
                mediaId: '@mediaId'
            }
            // ************
        }
    });
  });
查看更多
登录 后发表回答