AngularJS HTTP 使用响应
AngularJS HTTP use response
您好,这是我第二次使用 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 = { };
});
Return数据到.then
方法并保存promise:
var finalFilePromise = $http.get(cust_url)
.then(function (response) {
$scope.details = response.data;
//--> do multiple modify to response.data
FINAL_FILE = 'something';
return FINAL_FILE;
});
要使用,请从承诺中提取数据:
$scope.data = {
//USE HERE --> FINAL_FILE
finalFilePromise.then(function(FINAL_FILE) {
console.log(FINAL_FILE);
});
};
有关详细信息,请参阅
您好,这是我第二次使用 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 = { };
});
Return数据到.then
方法并保存promise:
var finalFilePromise = $http.get(cust_url)
.then(function (response) {
$scope.details = response.data;
//--> do multiple modify to response.data
FINAL_FILE = 'something';
return FINAL_FILE;
});
要使用,请从承诺中提取数据:
$scope.data = {
//USE HERE --> FINAL_FILE
finalFilePromise.then(function(FINAL_FILE) {
console.log(FINAL_FILE);
});
};
有关详细信息,请参阅