如何将数组参数传递给AngularJS $resource

时间:2021-08-13 21:18:35

When I pass an array to a resource action.. it doesn't convert the array parameter to an array of URL parameters

当我将数组传递给资源操作时。它不会将数组参数转换为URL参数数组

var Product = $resource('path/products');
Product.query({ids: [1,2,3]})

Instead of getting:

相反的:

path/products?ids[]=1&ids[]=2ids[]=3

I'm getting:

我得到:

path/products?ids=1&ids=2ids=3

Anyone knows how to get around this issue?

有人知道怎么解决这个问题吗?

2 个解决方案

#1


20  

You can use $resource to pass array

可以使用$resource传递数组

var searchRequest = $resource('/api/search/post', {}, {
    'get': {method: 'GET'}
});
searchRequest.get({'ids[]':[1,2,3]});

then you get request url

然后你得到请求url

/api/search/post?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3

you get %5B%5D instead of []

你得到%5B%5D而不是[]

and if you expect return array instead of object then you should use

如果你希望返回数组而不是对象,那么你应该使用

'get': {method: 'GET', isArray: true}

#2


-1  

Params should be declared like this

Params应该像这样声明

var Product = $resource('path/products?:ids',
{ids: '@ids'});

However I am not sure what resulting url you want to achieve. Any of the posted in OP ways is an invalid request, because of repeating parameter.

但是我不确定您想要实现什么结果url。任何以OP方式提交的请求都是无效的请求,因为重复参数。

To stick to GET verb and define an array in query params I see the only way: to construct param as a string

要坚持获取谓词并在查询params中定义数组,我看到的唯一方法是:将param构造为字符串

var query = [1,2,3].map(function(el){return 'brand[]='+el}).join('&');
Product.query({ids: query});

PS Unless you have strong reasons the best approach would be to send arrays using POST verb, like described in this post. With array sent over URL you can easily run out of maximum URL length

PS除非你有充分的理由,最好的方法是使用POST谓词发送数组,如本文所述。通过URL发送数组,可以很容易地耗尽URL的最大长度

#1


20  

You can use $resource to pass array

可以使用$resource传递数组

var searchRequest = $resource('/api/search/post', {}, {
    'get': {method: 'GET'}
});
searchRequest.get({'ids[]':[1,2,3]});

then you get request url

然后你得到请求url

/api/search/post?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3

you get %5B%5D instead of []

你得到%5B%5D而不是[]

and if you expect return array instead of object then you should use

如果你希望返回数组而不是对象,那么你应该使用

'get': {method: 'GET', isArray: true}

#2


-1  

Params should be declared like this

Params应该像这样声明

var Product = $resource('path/products?:ids',
{ids: '@ids'});

However I am not sure what resulting url you want to achieve. Any of the posted in OP ways is an invalid request, because of repeating parameter.

但是我不确定您想要实现什么结果url。任何以OP方式提交的请求都是无效的请求,因为重复参数。

To stick to GET verb and define an array in query params I see the only way: to construct param as a string

要坚持获取谓词并在查询params中定义数组,我看到的唯一方法是:将param构造为字符串

var query = [1,2,3].map(function(el){return 'brand[]='+el}).join('&');
Product.query({ids: query});

PS Unless you have strong reasons the best approach would be to send arrays using POST verb, like described in this post. With array sent over URL you can easily run out of maximum URL length

PS除非你有充分的理由,最好的方法是使用POST谓词发送数组,如本文所述。通过URL发送数组,可以很容易地耗尽URL的最大长度