How to pass parameters to a $http request service in Angular 1?

117 Views Asked by At

Let's say I have a foreign API URL like http://something.com/name/age/gender, where "name", "age" and "gender" are parameters.

As I need to retrieve that data from my Angular 1 App, I created a .service, that should use $http in order to get it. But... such service needs to be called from the controller, and needs to receive parameters.

How do I set all that and how do I call it from the controller?

The only thing I have in the service is this:

export const SearchPersonService = ($http) => {

    'ngInject';
    return $http.get('someUrl');



};

and the controller has the code below:

SearchPersonService.success(
    data => this.persons = data


).error(
    error => alert('err')
);
1

There are 1 best solutions below

0
On BEST ANSWER

Solved it! The only thing I had to do was wrapping the $http object into a function that takes the necessary parameters:

export const SearchPersonService = ($http) => {

    'ngInject';

    return function(params){   
        return $http.get('http://someurl/'+
            params.name+'/'+params.age+'/'+
            params.gender+'/'+params.status
        );
    }
};