Search code examples
angularjsangular-resource

$resource pass in array of values into url


I have a resource, with which I want to build I dynamic url. The url will contain query string parameters like so

/path?ids=1&ids=2&ids=23

Here is what I have tried. I call this by passing an array of strings to this resource such as

['1', '2', '23']

The problem is that the value passed to the addParameters method is not the array but the string ':ids'.

Is this even possible?

By the way, I know about the obvious error in the line "queryString += '&ids=' + id;"

define([], function () {
    'use strict';

    var addParameters = function (ids) {
        var queryString = '';
            _.each(ids, function (id) {
                queryString += '&ids=' + id;
            });
        return queryString;
    }

    var resource = function ($resource) {
        return $resource(
            '/path?' +addParameters(':ids'), {},
            {
                query:
                {
                    method: 'GET',
                    isArray: true
                }
            }
        );  
    };
    resource.$inject = ['$resource'];
    return resource;
});

Solution

  • Let's say your resource name is Test.

    Test.query({ids: [1, 2, 3]}).$promise.then(function(res... etc