javascript - AngularJS HTTP 使用响应

标签 javascript angularjs vis.js

您好,这是我第二次使用 AngularJS...我创建了一个 http 请求,没有问题。 但问题是,我如何获取此请求的结果或放入变量中?

var cust_url = "RETURN A JSON";    
var FINAL_FILE = "";

$http.get(cust_url)
    .then(function (response) {    
        $scope.details = response.data;   
        //--> do multiple modify to response.data      
        FINAL_FILE = 'something';
    }); 

$scope.data = {   
    /*USE HERE --> FINAL_FILE       
    return -> undefined on console if i try to access to FINAL_FILE
    */
};

就像这个例子......抱歉,我认为这是一个愚蠢的错误。谢谢你的时间。

最佳答案

$http 请求是异步的,这就是您获得未定义值的原因。如果您想使用响应数据,则必须在数据可用的 then 回调中执行此操作,如下所示:

$http.get(cust_url)
    .then(function (response) {

        $scope.details = response.data;

        //--> do multiple modify to response.data  

        FINAL_FILE = 'something';

        // here use the $scope.details or call a function to use data

        $scope.data = { };

});

关于javascript - AngularJS HTTP 使用响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60393153/

相关文章:

javascript - 更新 iframe 中的父 javascript 对象并在父级中获取 iframe 中的更新值

javascript - AngularJS 在 $http 调用后添加到 JSON 对象

javascript - AngularJS:计算 ng-repeat 的迭代次数

javascript - Vis js - 避免边缘重叠

javascript - 即使在 vis.js 示例中,字体超棒的图标也呈现为正方形

javascript - 在computed中导入组件和在vuejs动态导入中导入组件有什么区别?

javascript - lodash 将对象数组转换为单个键数组和多个值数组

javascript - 随着数字的增加,数字增量动画持续时间变慢

angularjs - Bootstrap-突出显示 Active 和 Angular 指令

javascript - Vis.js:以 react 风格添加 showPopup