Javascript 如何通过Ajax在控制器内部填充范围变量?

Javascript 如何通过Ajax在控制器内部填充范围变量?,javascript,data-binding,angularjs,controller,Javascript,Data Binding,Angularjs,Controller,在我当前的代码中,我使用了一种虚拟方法来获取数据。就像 var controlMeetings = $.ajax({ type: "GET", url: "./Info.xml", contentType: "text/xml", dataType: "xml", success: function (dataSource) { controlMeetings = Pu

在我当前的代码中,我使用了一种虚拟方法来获取数据。就像

var controlMeetings = $.ajax({
        type: "GET",
        url: "./Info.xml",
        contentType: "text/xml",
        dataType: "xml",
        success: function (dataSource) {           

            controlMeetings = PureJson(dataSource);         
        }
});

function MeetingsCtrl( $scope, $compile ) {

  $scope.meetings = controlMeetings;  
  $('#div1').html(
       $compile(
         '<ul><li ng-repeat="meeting in meetings"><a>{{meeting.count}}</a> <ul><li ng-repeat="child in meeting.children">{{child.meet}}</li></ul></li></ul>'
       )($scope)
     );
  $('#div1').prepend('<div class="mHeader">Race cources</div>');


}
var-controlMeetings=$.ajax({
键入:“获取”,
url:“./Info.xml”,
contentType:“text/xml”,
数据类型:“xml”,
成功:函数(数据源){
controlMeetings=PureJson(数据源);
}
});
函数会议SCTRL($scope,$compile){
$scope.meetings=控制会议;
$('#div1').html(
$compile(
“
  • {{meeting.count}
    • {{{child.meeting}}
    ” )($scope) ); $('#div1')。prepend('Race cources'); }

这显然不好(是的,我为这段代码感到羞耻),但它可以工作。问题是如何在控制器中准确地填充$cope.meetings变量并避免使用全局变量?

我试图使用AngularJS方法重新编写您的示例

控制器:

function MeetingsCtrl ($scope) {

  $http.get('./Info.xml').success(function (data) {
      $scope.meetings = data;
    });

}
查看文件:

<div id="div1">
  <div class="mHeader">Race cources</div>
  <ul>
    <li ng-repeat="meeting in meetings">
      <a>{{meeting.count}}</a>
        <ul><li ng-repeat="child in meeting.children">{{child.meet}}</li></ul>
    </li>
  </ul>
</div>

种族歧视
  • {{meeting.count}
    • {{child.meet}

我想你可以从角度出发,遵循上的教程-我得到“错误:$http未定义”,在我添加函数MeetingsCtrl($scope,$compile,$http){$http.get('./Info.xml')。成功(函数(数据){$scope.meetings=data;});然后我在jquery 1.9.1中开始出现长时间运行的脚本异常。我忘了注入
$http
,但我的坏!
$compile
不是必需的!如果使用jquery的
.html()
若要将HTML添加到网页中,您的做法不对,请使用单独的部分视图文件!我不确定您的
Info.xml
文件的外观(我使用静态数据只是为了说明其工作原理),但请看这个:但我的想法是在另一个页面中重用控制器,每次手动注入html。我应该每次直接键入html吗?