【问题标题】:Angular $Scope Object run before the Jquery get method finish getting the data?Angular $Scope Object 在 Jquery get 方法完成获取数据之前运行?
【发布时间】:2013-09-28 12:34:07
【问题描述】:

我有这段代码来获取 xml 数据并将其转换为 json 最后将 json 分配给 $scope 对象以由视图处理

function employeesList($scope) {
    $.get('http://www.benisuef.gov.eg/_vti_bin/owssvr.dll?Cmd=Display&List=%7B9E8B17D5-7AE8-4BC8-9068-105DA949734A%7D&XMLDATA=TRUE', function(xml) {
        var json = $.xml2json(xml, true);
        $scope.employeeList = json.data[0].row;
    });
}

问题是角度控制器没有等到 get 完成它的工作 我怎样才能让它等待呢??!

【问题讨论】:

  • A 之所以这样调用 JAX,是因为它是 异步
  • 你不应该使用 jQuery Ajax 而是使用 Angularjs $http 方法。

标签: jquery xml json angularjs get


【解决方案1】:

如果我开始使用Angular,我会用 Angular 编写所有模块(更少的问题)。如您所知,Angular $http 返回 promise 就像 jQuery 中的回调一样。

你可以阅读这个link promises 是如何工作的。

顺便说一句,你可以写类似

$scope.employeeList = $http.get('http://www.benisuef.gov.eg/_vti_bin/owssvr.dll?Cmd=Display&List=%7B9E8B17D5-7AE8-4BC8-9068-105DA949734A%7D&XMLDATA=TRUE')).then(
   function(result) {
     var json = $.xml2json(result.data, true);

   return json.data[0].row;
  });

一些调试器:

<pre>{{employeeList  | json}}</pre>

【讨论】:

    猜你喜欢
    • 2019-02-27
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2021-06-15
    • 2016-03-13
    • 2021-03-28
    • 2021-05-09
    相关资源
    最近更新 更多