0

i have this code to get the xml data and convert it to json finally assign the json to the $scope object to be handled by the view

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;
    });
}

the problem is the angular controller doesn't wait until the get finish its work how can i make it wait for that ??!

4

1 回答 1

1

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

你可以阅读这个链接,promise 是如何工作的。

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

$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>
于 2013-09-28T13:14:21.310 回答