简体   繁体   English

在全局变量中存储angular js响应的问题

[英]Issue in storing angular js response in global variable

I am making an http call to my controller through angular.js and trying to store the response in one global variable like below. 我正在通过angular.js对我的控制器进行http调用,并尝试将响应存储在一个全局变量中,如下所示。

app.controller('dashBoardController', ['$scope', '$http', '$location',
    function ($scope, $http, $location) {
        $scope.details = [];
        var init = function () {
            $http({
                method: 'GET',
                url: urlpath + '/values'
            }).success(function (response) {
                $scope.workouthistory = JSON.stringify(response);
                $scope.details = response;
                console.log($scope.details)
            });
        }
        init();

        alert($scope.details)
        ]);

But when I try to print the response that I stored globally in an array I am getting an blank response. 但是,当我尝试打印全局存储在数组中的响应时,得到的响应为空。 But, the console inside the method prints the response correctly. 但是,方法内的控制台可以正确打印响应。 Kindly, help me figure out how to store this response globally. 请帮我弄清楚如何在全局存储此响应。

Your alert code runs before the function where you set the variable and have the console.log. 您的警报代码在设置变量并具有console.log的函数之前运行。 The function inside your success() call is a callback function which runs when the http request comes back with a response. 您的success()调用中的函数是一个回调函数,该函数在http请求返回响应时运行。

You can try moving your alert message into a new global function, and then call that function inside your success callback. 您可以尝试将警报消息移到新的全局函数中,然后在成功回调中调用该函数。

First, going with good programming practice global variables in angular should be defined using services. 首先,应遵循良好的编程习惯,使用服务定义角度的全局变量。 Scope properties would not be accessible at other places. 范围属性将无法在其他位置访问。 Second, these kind of tasks can be executed using promises, like below 其次,可以使用promise执行此类任务,如下所示

(function () {
    var app = angular.module("myApp", []);

    app.constant("globalVariableSvc", { details: [] });

    app.controller("dashboardCtrl", function ($scope, $q, $http, $location, globalVariableSvc) {

        //creating a promise
        function getValues() {
            var dfd = $q.defer();
            $http.get(urlpath + '/values')
                .success(function (response) {
                    dfd.resolve(response);
                })
                .error(function (response) {
                    dfd.reject(response);
                });
            return dfd.promise;
        }

        function init() {
            globalVariableSvc.details = [];

            //using promise
            getValues()
                .then(function (response) {

                    // when promise is being resolved or it has succeeded to perform the task
                    $scope.workouthistory = JSON.stringify(response);
                    globalVariableSvc.details = response;
                    console.log(globalVariableSvc.details)
                    alert(globalVariableSvc.details);

                }, function (response) {
                    // when promise is being rejected or it has failed to perform the task
                    alert(globalVariableSvc.details);
                });
        }

        init();
    });
}());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM