如何以角度方式将参数传递给服务中的函数

时间:2017-02-24 12:32:18

标签: javascript angularjs angular-services

我试图以角度方式将参数传递给服务,但是当我这样做时,角度会完全断开。

有人可以告诉我我做错了吗?

这是我的服务:

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);

控制器功能从我尝试传递参数

// 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;
    });

更新 这是我得到的错误

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)

更新2 这就是我想要做的: 在C#中,您可以执行以下操作:使用函数创建服务,而不是像myService.myFunction(myPararameters)一样调用它,这是我想用angular实现的,但我不知道如何

1 个答案:

答案 0 :(得分:1)

我不知道你在哪里引用代码,但我还没有看到这种结构。

以下是每个人用来定义工厂的原因

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);