我遇到以下angularjs功能:javascript函数不返回未定义值
this.loadDocument = function() {
var documents = [];
alert("load documents");
$http({
url: 'http://localhost:8085/api/jsonws/dlapp/get-file-entries/repository-id/10182/folder-id/0',
method: 'get',
headers: {
"Authorization": this.makeAuth(this.username,this.password)
}
}).success(
function(response) {
alert("sucess");
documents=response;
});
return documents;
};
我把它叫做从下面的代码:
$scope.loadDocument = function() {
$scope.documents=documentService.loadDocument();
}
但是从函数的返回值是undefined.because返回ajax调用执行成功之前的值。
有没有解决方法?
在此先感谢。
您可以通过回调*出于某种原因*使用此机会,并在相应的回调中进行处理。的 – 2015-03-19 11:40:49
可能重复[如何返回从异步调用的响应?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – JJJ 2015-03-19 11:40:56