AngularJS $resource url causes “question mark” ? t

2019-06-23 23:27发布

I have a $resource that defines a custom url for the :all method.

angular.module('MyApp').
  factory('Object', ['$resource', ($resource) ->
    $resource(
      '/api/groups/:group_id/objects/:id.json',
      {
        id: '@id',
        group_id: '@group_id'
      },
      all: {
        method: 'GET',
        url: '/api/objects/all.json'
      }
    )
  ])

When my page loads, the request goes out to '/api/objects/all.json?'. It's loading correctly, but the presence of the ? is confusing to me. I didn't pass it any parameters, so why does angular add the ? to the request?

Can I get rid of it somehow?

2条回答
时光不老,我们不散
2楼-- · 2019-06-24 00:05

You should move your top level parameters into the method that will be expecting an id into the definition of a get method. Since you have { id: @id } where it is, the resource service is thinking you'll be adding an id and group id to the call.

查看更多
贼婆χ
3楼-- · 2019-06-24 00:26

Since angular v.1.2.14 this issue has been fixed. Update to it and have a clear url without unnecessary question

查看更多
登录 后发表回答