$ http内的变量不反映块外的变化

时间:2013-07-11 21:04:10

标签: http web-applications angularjs angular-services

我正在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服务? 谢谢!

1 个答案:

答案 0 :(得分:0)

我认为如果没有某种解析器,你可以像JSON一样直接插入XML。

在AngularJS中查看处理XML的问题:How to handle XML services in AngularJS?

具体检查这些模块/插件以提供帮助:

相关问题