gpt4 book ai didi

javascript - AngularJS HTTP 使用响应

转载 作者:行者123 更新时间:2023-12-01 00:06:00 25 4
gpt4 key购买 nike

您好,这是我第二次使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com