【发布时间】:2013-07-12 05:12:44
【问题描述】:
我正在用 Angular 编写一个 Web 应用程序,这段代码旨在从服务器获取测试 XML 数据作为文本。我正在使用 Angular 的 $http 服务从服务器发送 GET 请求。变量 'xmlData' 在 $http 块内被更改,但在该块之外,它的值仍然是默认值。代码如下:
this.getData = function() {
var xmlData = "Default";
var xmlDoc;
// send an http GET request for the XML text data
$http.get('http://localhost:1337/testXML.txt').success(function(data) {
xmlData = data;
if (xmlData == null) {
alert("Data Error Occurred");
}
// here, 'xmlData' contains the correct information.
}).
error(function() {
alert("HTTP Error Occurred");
});
// once the above $http block ends, the xmlData has the default value.
return xmlData;
}
所以这只是一个愚蠢的范围问题吗?还是我错误地使用了 Angular 的 $http 服务? 谢谢!
【问题讨论】:
标签: http web-applications angularjs angular-services