Javascript 在angularjs中的服务中的函数之间传递变量

Javascript 在angularjs中的服务中的函数之间传递变量,javascript,angularjs,Javascript,Angularjs,我一直在寻找答案,但似乎什么也找不到。我有一个服务,在第一个块中,我成功地记录了一个url,然后需要将它传递到我的getData()函数中。但是它没有定义,我尝试了下面的方法,我尝试将第一个$http.get移动到调用它的控制器中,以及将第一个$http.get移动到getData()函数中。我这样做完全错了吗 di.service('testService', function($http) { $http.get('https://us.api.data/tichondrius?lo

我一直在寻找答案,但似乎什么也找不到。我有一个服务,在第一个块中,我成功地记录了一个url,然后需要将它传递到我的
getData()
函数中。但是它没有定义,我尝试了下面的方法,我尝试将第一个
$http.get
移动到调用它的控制器中,以及将第一个$http.get移动到
getData()
函数中。我这样做完全错了吗

di.service('testService', function($http) {
    $http.get('https://us.api.data/tichondrius?locale=en_US&apikey=xxxxxxxx').
    then(function(response) {
        var urlToJsonFileUncut = response.data.files[0].url;
        console.log(urlToJsonFileUncut);
        urlToJsonFile = urlToJsonFileUncut.slice(7);
        console.log(urlToJsonFile);
        return urlToJsonFile;
    });
    this.getData = function(urlToJsonFile) {
        console.log(urlToJsonFile);
        return $http.get('http://localhost:1337/' + urlToJsonFile).
    then(function(response) {
        console.log(response.data.realms[0].name);
        return response.data.realms[0].name;
    });
}});

$http
是一个异步请求。因此,您需要将其链接到第一个请求中,以确保在调用第二个请求时第一个响应的值可用

di.service('testService', function($http) {
  var getData = function () {
    return $http.get('https://us.api.data/tichondrius?locale=en_US&apikey=xxxxxxxx').
    then(function(response) {
      var urlToJsonFileUncut = response.data.files[0].url;
      console.log(urlToJsonFileUncut);
      var urlToJsonFile = urlToJsonFileUncut.slice(7);
      console.log(urlToJsonFile);

      $http.get('http://localhost:1337/' + urlToJsonFile).
      then(function(response) {
        console.log(response.data.realms[0].name);
        return response.data.realms[0].name;
      });
    });
  }

  return { getData: getData; }
});

$http
是一个异步请求。因此,您需要将其链接到第一个请求中,以确保在调用第二个请求时第一个响应的值可用

di.service('testService', function($http) {
  var getData = function () {
    return $http.get('https://us.api.data/tichondrius?locale=en_US&apikey=xxxxxxxx').
    then(function(response) {
      var urlToJsonFileUncut = response.data.files[0].url;
      console.log(urlToJsonFileUncut);
      var urlToJsonFile = urlToJsonFileUncut.slice(7);
      console.log(urlToJsonFile);

      $http.get('http://localhost:1337/' + urlToJsonFile).
      then(function(response) {
        console.log(response.data.realms[0].name);
        return response.data.realms[0].name;
      });
    });
  }

  return { getData: getData; }
});

我建议您使用工厂而不是服务

查看以下代码

di.factory('testService', function ($http) {
    var variable_name;

    var serviceMethodName = function () {
        $http.get('https://us.api.data/tichondrius?locale=en_US&apikey=xxxxxxxx').
        then(function (response) {
            var urlToJsonFileUncut = response.data.files[0].url;
            console.log(urlToJsonFileUncut);
            urlToJsonFile = urlToJsonFileUncut.slice(7);
            console.log(urlToJsonFile);
            variable_name = urlToJsonFile;          //added 
        });
    }

    //modified parameter in below method
    var getData = function (variable_name) {

        var urlToJsonFile = variable_name;          //added 
        console.log(urlToJsonFile);
        return $http.get('http://localhost:1337/' + urlToJsonFile).
    then(function (response) {
        console.log(response.data.realms[0].name);
        return response.data.realms[0].name;
    });

    }

    //Exposes the two methods and accessbile through out the app unless it is modified
    return {
        serviceMethodName: serviceMethodName,
        getData:getData
    }

});

我建议您使用工厂而不是服务

查看以下代码

di.factory('testService', function ($http) {
    var variable_name;

    var serviceMethodName = function () {
        $http.get('https://us.api.data/tichondrius?locale=en_US&apikey=xxxxxxxx').
        then(function (response) {
            var urlToJsonFileUncut = response.data.files[0].url;
            console.log(urlToJsonFileUncut);
            urlToJsonFile = urlToJsonFileUncut.slice(7);
            console.log(urlToJsonFile);
            variable_name = urlToJsonFile;          //added 
        });
    }

    //modified parameter in below method
    var getData = function (variable_name) {

        var urlToJsonFile = variable_name;          //added 
        console.log(urlToJsonFile);
        return $http.get('http://localhost:1337/' + urlToJsonFile).
    then(function (response) {
        console.log(response.data.realms[0].name);
        return response.data.realms[0].name;
    });

    }

    //Exposes the two methods and accessbile through out the app unless it is modified
    return {
        serviceMethodName: serviceMethodName,
        getData:getData
    }

});

这对我来说也很有效,而且我还需要能够将变量从ng模型传递到服务(或工厂),是否需要更改其中哪一个更可行?是的,您可以非常简单地将服务注入控制器,并使用服务变量分配给$scope
$scope.variable=testService.variable
这对我来说也很有效,而且我还需要能够将变量从ng模型传递到服务(或工厂),需要这样做会改变哪一个更可行吗?是的,您可以非常简单地将您的服务注入控制器并使用服务变量分配给$scope<代码>$scope.variable=testService.variable