【问题标题】:Variable inside $http not reflecting change outside block$http 内的变量不反映块外的变化
【发布时间】: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


    【解决方案1】:

    我认为没有某种解析器就不能像 JSON 那样直接插入 XML。

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

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

    【讨论】:

    • 好的,谢谢!我使用解析器跟踪此代码,该解析器将 xml 数据转换为 javascript 可以使用的 DOM。但是你是说http请求不能处理xml?
    • 你可以用 $http 抓取任何东西。但只有 JSON 可以像您设置的那样本机附加到变量。
    猜你喜欢
    • 2012-08-18
    • 2021-05-21
    • 1970-01-01
    • 2011-12-19
    • 2018-09-23
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多