如何在http调用角后更新全局变量

时间:2021-08-28 23:22:56

I have a global variable which I need to update after http get call. After update I need to pass this updated variable to other function. I can't understand what is the best way to do this. Here is my code:

我有一个全局变量,需要在http get调用之后更新它。更新后,我需要将更新后的变量传递给其他函数。我不明白做这件事最好的办法是什么。这是我的代码:

    app.controller('myCtrl', function($scope, $http) {
     var data = '';
     $http.get("data.json")
       .then(function(response) {
        data = response.data;
     });
     vm.mapOptions = {
        controls: {
            navigator: false
        },
        center: [40, -35],
        zoom: 3,
        layers: [{
            style: {
                fill: {
                    color: '#1996E4'
                },
                stroke: {
                    color: '#FFFFFF'
                }
            },
            type: 'shape',
            dataSource: data
        }],
        shapeCreated: onShapeCreated,
        shapeFeatureCreated: onShapeFeatureCreated
    };
    });

Is it possible at all to update global variable after http call?

是否可以在http调用之后更新全局变量?

Thank you for help in advance.

感谢您的帮助。

1 个解决方案

#1


2  

When you do a http request, it takes some time to send the request and get the response, specially when you send the request to a API on a server,but in meanwhile the execution continues immediately and the statement after your http call is executed and if you have something that depends on the response, most properly will be failed.

做一个http请求时,需要一定的时间来发送请求和响应,特别当你发送请求到服务器的API,但是在同时执行持续立即声明执行http调用后如果你有东西取决于响应,最恰当地将失败。

in your case vm.mapOptions have dependency to data, which is a local variable getting the respnse from get request. so what can you do?

在你的虚拟机。mapOptions依赖于数据,这是一个本地变量,可以从get请求中获得respnse。那么你能做什么呢?

Step 1 :

步骤1:

make a function for all codes that are involve with your response :

为与您的响应相关的所有代码创建一个函数:

$scope.myCallBack = function(data){
  vm.mapOptions = {
    controls: {
        navigator: false
    },
    center: [40, -35],
    zoom: 3,
    layers: [{
        style: {
            fill: {
                color: '#1996E4'
            },
            stroke: {
                color: '#FFFFFF'
            }
        },
        type: 'shape',
        dataSource: data
    }],
    shapeCreated: onShapeCreated,
    shapeFeatureCreated: onShapeFeatureCreated
   };

}

Step 2 : Call the myCallBack function inside your $http.get right after getting the response

步骤2:在$http中调用myCallBack函数。得到回应后马上行动。

var data = '';
 $http.get("data.json")
   .then(function(response) {
    data = response.data;
    $scope.myCallBack(data);
 });

#1


2  

When you do a http request, it takes some time to send the request and get the response, specially when you send the request to a API on a server,but in meanwhile the execution continues immediately and the statement after your http call is executed and if you have something that depends on the response, most properly will be failed.

做一个http请求时,需要一定的时间来发送请求和响应,特别当你发送请求到服务器的API,但是在同时执行持续立即声明执行http调用后如果你有东西取决于响应,最恰当地将失败。

in your case vm.mapOptions have dependency to data, which is a local variable getting the respnse from get request. so what can you do?

在你的虚拟机。mapOptions依赖于数据,这是一个本地变量,可以从get请求中获得respnse。那么你能做什么呢?

Step 1 :

步骤1:

make a function for all codes that are involve with your response :

为与您的响应相关的所有代码创建一个函数:

$scope.myCallBack = function(data){
  vm.mapOptions = {
    controls: {
        navigator: false
    },
    center: [40, -35],
    zoom: 3,
    layers: [{
        style: {
            fill: {
                color: '#1996E4'
            },
            stroke: {
                color: '#FFFFFF'
            }
        },
        type: 'shape',
        dataSource: data
    }],
    shapeCreated: onShapeCreated,
    shapeFeatureCreated: onShapeFeatureCreated
   };

}

Step 2 : Call the myCallBack function inside your $http.get right after getting the response

步骤2:在$http中调用myCallBack函数。得到回应后马上行动。

var data = '';
 $http.get("data.json")
   .then(function(response) {
    data = response.data;
    $scope.myCallBack(data);
 });