【问题标题】:Make function in rootscope return in scope使 rootscope 中的函数在范围内返回
【发布时间】:2017-02-06 02:55:48
【问题描述】:

我已经制作了一个带有属性的 rootscope 函数。它正在从数据库中获取一些数据。

我想在控制器中使用这个 rootscope 函数,并将我得到的数据放入一个变量中。

$rootScope.get_option = function(option_name){
        $http.post("server/read.php",{'subject': "options", "args": option_name })
        .success(function (response) {
            console.log(response);
            $rootScope.option_get_value = response;

        });

        if($rootScope.option_get_value){
            return $rootScope.option_get_value;
        }

    }

这就是我在控制器中拥有的东西

    $scope.subscription.reduced_hourrate = $rootScope.get_option('verlaagd_tarief');
    console.log($scope.subscription.reduced_hourrate);

当我运行脚本时,我在日志中看到 $rootScope 函数返回了正确的值。但是范围给了我未定义的数据。

为什么会这样?有人帮我给我一些建议吗?

【问题讨论】:

  • 不要使用$httpsuccess()方法。使用.then

标签: angularjs rootscope


【解决方案1】:
$rootScope.get_option = function(option_name){
    return $http.post("server/read.php",{'subject': "options", "args": option_name    })
    .success(function (response) {
        console.log(response);
        if(response){
           return response;
        }
    });
}

当您调用 $http.post 时,在调用 if 行后一段时间返回成功,这就是您得到“未定义”的原因,因为响应没有返回填充 $rootScope 的数据。 option_get_value。 您正在使用“.success”($http 中的一个承诺:https://docs.angularjs.org/api/ng/service/$http), 当您将 return 移入 promise 时,它​​将仅在响应可用时启动。

在控制台中,您可以看到响应中的完整数据,因为控制台正在通过引用工作,这意味着...当您单击日志打开 obj 时,返回已经返回并且它正在引用数据到控制台。

【讨论】:

  • 你的return response不能在任何地方使用。
【解决方案2】:

$http 进行异步调用。这意味着您的 if 语句 if($rootScope.option_get_value) ... 在您的 http 成功函数解析之前被调用。

要让它工作,你可以这样做

$rootScope.get_option = function(option_name){
    return $http.post("server/read.php",{'subject': "options", "args": option_name })
}

然后在你的控制器内部

$rootScope.get_option('verlaagd_tarief').success(function (response) {
     $rootScope.option_get_value = response;
});

但我不确定这是向控制器传递数据的最佳方式。 一种常见的方法是使用services。不使用$rootScope

这是一个如何使用服务将数据传递给控制器​​的示例。

var app = angular.module('myApp', []);

//declare a service that make the http calls
myApp.factory('myHttpService', function($scope, $http) {

   //return the public API
    return {
        //use a callback function to return
        //the result when the promise is resolved
        get_option : function(option_name, fct){
            $http.post("server/read.php",
                 {
                      "subject": "options", 
                      "args": option_name 
                 }
            ).then(function(result){
                fct(result) //calling the callback when the promise is resolved to return the result
            })
    }
});

// the controller using myHttpService
myApp.controller('myCtrl', function($scope, myHttpService) {
   myHttpService.getOption('verlaagd_tarief', function(result){
       $scope.option_get_value = result
   })
});

【讨论】:

  • 为什么你在第一个答案中使用 Promise,而不是第二个答案?
  • 在第二个示例中,我使用了在then 函数内部调用的回调函数,我只是将.success 替换为.then,这是我通常的做法。使用.then,您无需返回result.data,而只需返回result,请参阅此帖子了解stackoverflow.com/questions/16385278/… 的区别
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 2012-07-29
  • 2019-11-11
相关资源
最近更新 更多