以角度为单位访问Factory外部回调函数内的变量

时间:2021-12-12 12:46:55

My Factory looks like this:

我的工厂看起来像这样:

.factory('MyFactory', function(){
    return: {
         someFunction: functon(firstParam, secondParam, resultObject) {
                     $http.get(url).success(resultObject);
        }     
    }
})

My controller looks like this:

我的控制器看起来像这样:

.controller('MyCtrl', function($scope) {
    MyFactory.someFunction(paramOne, paramTwo, function(resultObj) {
          $scope.result = resultObj;
          console.log($scope.result) //has the object
     });
    console.log($scope.result) //undefined
}):

I'd like $scope.result to be available inside the controller and not just within the callback. Right now its only available within the callback function.

我希望$ scope.result可以在控制器内部使用,而不仅仅是在回调中。现在它只在回调函数中可用。

I understand what the problem is here, that the scope of the variable inside the callback function ends within the function, I just don't understand how to overcome this.

我理解这里的问题是,回调函数内部变量的范围在函数内结束,我只是不明白如何克服这个问题。

1 个解决方案

#1


2  

The problem is not that the scope of the variable is within the callback function, the problem is that while you do console.log($scope.result) outside the function inside the controller the $http request has not been completed so it will be undefined.

问题不在于变量的范围在回调函数内,问题是当你在控制器内的函数外部执行console.log($ scope.result)时,$ http请求尚未完成,因此它将是未定义。

Once the request is complete you can access $scope.result within the controller. The best way to solve this is to ensure that the $http request is finished before your controller even loads and this is possible through use of the resolve function in the routes. You would fire off the $http request and wait for it to complete before even loading the route, so the result could be directly injected into the controller.

请求完成后,您可以访问控制器中的$ scope.result。解决此问题的最佳方法是确保在控制器加载之前完成$ http请求,这可以通过在路由中使用resolve函数来实现。您将启动$ http请求并在加载路由之前等待它完成,因此结果可以直接注入控制器。

Here is a link that explains this concept: http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

这是一个解释这个概念的链接:http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

#1


2  

The problem is not that the scope of the variable is within the callback function, the problem is that while you do console.log($scope.result) outside the function inside the controller the $http request has not been completed so it will be undefined.

问题不在于变量的范围在回调函数内,问题是当你在控制器内的函数外部执行console.log($ scope.result)时,$ http请求尚未完成,因此它将是未定义。

Once the request is complete you can access $scope.result within the controller. The best way to solve this is to ensure that the $http request is finished before your controller even loads and this is possible through use of the resolve function in the routes. You would fire off the $http request and wait for it to complete before even loading the route, so the result could be directly injected into the controller.

请求完成后,您可以访问控制器中的$ scope.result。解决此问题的最佳方法是确保在控制器加载之前完成$ http请求,这可以通过在路由中使用resolve函数来实现。您将启动$ http请求并在加载路由之前等待它完成,因此结果可以直接注入控制器。

Here is a link that explains this concept: http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

这是一个解释这个概念的链接:http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx