0

I'm trying to pass parameters to a service in angular, but angular completly breaks when I am doing it.

Can someone please tell me what I am doing wrong?

This is my service:

function AlertData($http){


var AlertData = {};
  var settings = {
    }
  }

  // Registering client by its reference, housnumber and postalcode
  AlertData.getData = function(reference, housnumber, postalcode) {//These paramaters are not comming through
    settings.url = "url";
    settings.data = { "data": { "attributes": { "reference": reference, "huisnummer": housnumber, "postcode": postalcode } } };
    return $http(settings);
  };

  return AlertData;
}

alertApp.factory('alertData', AlertData);

And the controller function where from I try to pass the parameters

// Registering client
  alertData.getData(clientReference, housnumber, post)
    .then(function(data){
      if(data.status==201){
        getClientByReference();
      } else {
        $scope.dataError = standaardError;
      }
    })
    .catch(function(data, status, headers, config){
      $scope.dataError = standaardError;
    });

UPDATE This is the error that i'm getting

Error: [ng:areq] Argument 'fn' is not a function, got Object
http://errors.angularjs.org/1.4.7/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20Object
    at angular.js:68
    at assertArg (angular.js:1796)
    at assertArgFn (angular.js:1806)
    at Function.annotate [as $$annotate] (angular.js:3765)
    at Object.invoke (angular.js:4456)
    at Object.enforcedReturnValue [as $get] (angular.js:4330)
    at Object.invoke (angular.js:4478)
    at angular.js:4295
    at getService (angular.js:4437)
    at Object.invoke (angular.js:4469)

UPDATE 2 This is what i want to do: In C# you can do something like this: Make a service with functions than call it like this myService.myFunction(myPararameters) this is what i like to achieve with angular, but I don't know how

apero
  • 1,074
  • 2
  • 23
  • 38
  • Breaks? can you be MORE clear pls, i.e.: any errors? – Jax Feb 24 '17 at 12:37
  • You shouldn't call AlertData. – Estus Flask Feb 24 '17 at 12:38
  • @Jax please see my updated question – apero Feb 24 '17 at 12:39
  • @estus I don't really understand what you telling – apero Feb 24 '17 at 12:40
  • Refer: http://stackoverflow.com/questions/12505760/processing-http-response-in-service – Avantika Saini Feb 24 '17 at 12:46
  • You call it with AlertData() and you shouldn't do that. How did you expect it to work? This will make $http undefined and return an object in the place where a function is expected. That's exactly what error message is saying. – Estus Flask Feb 24 '17 at 12:51
  • @estus sorry but I really dont understand what I have to do. Im very new to angular. In C# you can do something like this: Make a service with functions than call it like this myService.myFunction(myPararameters) this is what i like to achieve here – apero Feb 24 '17 at 13:04
  • you are missing a `get`. it should be `return $http.get(settings);` – Emin Laletovic Feb 24 '17 at 13:09
  • 1
    You can do this in JS too. But since Angular is a framework, you need to follow its interfaces. `factory` expects a function as the second argument. That's what the answer says. – Estus Flask Feb 24 '17 at 13:13

1 Answers1

1

I don't know where you're referring the code but I haven't seen this kind of structure.

Below is what in general everyone use to define a factory

function AlertData($http) {
    var obj = {};
    // Registering client by its reference, housnumber and postalcode
    obj.getData = function(reference, housnumber, postalcode) {
        settings.url = "url";
        settings.data = {
            "data" : {
                "attributes" : {
                    "reference" : reference,
                    "huisnummer" : housnumber,
                    "postcode" : postalcode
                }
            }
        };
        return $http(settings);
    }

    return obj;
}

alertApp.factory('alertData', AlertData);
Gangadhar Jannu
  • 4,136
  • 6
  • 29
  • 49
  • ahaaaa now I understand what I did wrong. I was calling the function from inside it self the whole time. Changing AlerData to obj solved the problem. THank you very much – apero Feb 24 '17 at 13:11
  • 1
    I'm glad my solution helped. Refer john papa's [angular style guide](https://github.com/johnpapa/angular-styleguide/tree/master/a1) for best practices – Gangadhar Jannu Feb 24 '17 at 13:31