Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array parameter to AngularJS $resource

Tags:

angularjs

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

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?

like image 474
chrony Avatar asked Apr 01 '15 10:04

chrony


1 Answers

You can use $resource to pass array

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

then you get request url

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

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

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

'get': {method: 'GET', isArray: true}
like image 184
Shaishab Roy Avatar answered Oct 02 '22 15:10

Shaishab Roy