AngularJS访问变量超出功能

时间:2018-09-17 13:39:41

标签: javascript angularjs http model-view-controller

我有一个如下所示的控制器:

.controller('googleFormsCtrl', function ($scope, $http, $window) {
    // SEND THE HTTP REQUEST
    var url;
    var allForms;
    var externalWindow;
    // MAKE API REQUEST for Authorization
    $http.get('/api/googleAuth').then(function (response) {
        url = response.data;
    });

    this.googleAuthentication = function () {
        externalWindow = $window.open(url, "Please Sign in with Google", "width:350px,height:350px");
    };

    // Close Authorization and set credentials
    window.onmessage = function (info) {
        externalWindow.close();
        // Get the CODE from the URL
        var urlCode = info.data;
        // Get pure code
        var idx = urlCode.lastIndexOf("code=");
        var code = urlCode.substring(idx + 5).replace("#","");
        // Get TOKEN
        $http.get('/api/googleToken?code=' + code).then(function (response) {

            console.log(response.data);
            allForms = response.data;

        });

    };

    console.log("HERE IS UNDEFİNED " + allForms);

    // Fill Drop Down with Forms
    this.fillForms = function () {

    }
})

我有一个window.onmessage函数,在其中有一个http GET记录,可以检索数据给我。我试图通过将其值分配给“ allForms”变量来检索它,以便ı可以在控制器中的另一个函数中使用它,但是它检索未定义的值。如何检索它?预先感谢。

0 个答案:

没有答案