我有下面的函数,它有一个 Angular http请求。在 success
部分,我试图返回提供的数据,并返回我的 setTextFile()
中的值函数,但它是未定义的。我已检查数据不为空或未定义,因此我知道它包含信息,但我不明白为什么它不返回该内容。
var showFile = function (txtFile, host) {
var datData;
var url = 'rest/archive-viewer/spanish-discount/' +
$rootScope.currentEnv + '/' +
host + '/' +
txtFile;
$http.get(url).success(function (data) {
$scope.sendingMessage = "Getting file";
$scope.sending = false;
$scope.sendingMessage = "";
return data;
}).error(function (data) {
$scope.sendingMessage = "Creation failed";
$scope.sending = false;
});
}
$scope.pullFiles();
};
这是我的setTextFile
由于 showFile()
的结果,函数的返回值未定义返回值。
var setTextFile = function (file,host) {
console.log("Is this null " + showFile(file, host));
return showFile(file, host);
}
最佳答案
该 http 请求是异步的。我建议您做的是将数据分配给可验证的范围,例如: $scope.setTextFile = 您在ajax请求中传递的成功函数中的数据。相反,您可以创建一个函数来设置数据并触发 success 函数内的函数。
关于javascript - 返回数据在 Angular 的 http 请求中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31326571/