Search code examples
angularjsangular-resource

AngularJS $resource multiple optional parameters


I have a webservice that allows me to get search results:

http://url/api/search/?params

Possible parameters to use are: name, location and age They can be used in any order and combination (they are optional) and are case insensitive (parameters and values)

example:

http://url/api/search/?aGe=15&NaME=jOE THomaS&LOCatioN=BElgIUm

I have no idea how to setup my $resource correctly to get this working. Obviously, all the possibilities are GET methods.


Solution

  • You can just call it directly since they're query params.

     angular.module('app').factory('MyService', ['$resource', '$window',function ($resource, $window) {
            'use strict';
            return $resource('http://url/api/search/');
        }]);
    

    Then call it like this

    $scope.result = MyService.get({age: 'foo', location: 'bar'}, 
                           function success() {// success handler here}, 
                           function fail(resp) {// failure handler here});