0

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')
);
Captain
  • 105
  • 1
  • 15
  • It would be better if you could post your code here. Whatever you've tried. – Akash Agrawal Dec 11 '17 at 12:04
  • @AkashAgrawal here is some code, it's not much: `export const SearchPersonService = ($http) => { 'ngInject'; return $http.get(''someurl"); };` – Captain Dec 11 '17 at 12:05
  • can't you edit your string in controller? --> `"http://something.com/"+$scope.name+"/"+$scope.age+"/"+$scope.gender` – Aleksey Solovey Dec 11 '17 at 12:08
  • I could solve it! The service was returning an $http object, and the only thing I should do is wrap the $http into a function that takes parameters, that will be converted in the right URL. Thank you @AlekseySolovey for your patience! – Captain Dec 17 '17 at 10:14

1 Answers1

0

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
        );
    }
};
Captain
  • 105
  • 1
  • 15