2013-04-30 81 views
0

在我当前的代码中,我使用虚拟方式来获取数据。它就像如何通过Ajax填充范围变量进入控制器?

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


} 

它显然不好(是我从这个代码感到羞耻),但它的工作atm。问题是如何在控制器中实际填充$ cope.meetings变量并避免使用全局变量?

+0

我想你可以用角方式接地做,请按照教程 - http://egghead.io – Neil 2013-04-30 08:23:21

回答

1

我试着用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> 
+0

我“错误:$ HTTP没有定义” 和i之后加入功能MeetingsCtrl($范围,$编译,$ HTTP){ $ http.get( './ Info.xml')成功(功能(数据){。 $ scope.meetings = data; });然后我开始在jquery 1.9.1中长时间运行脚本异常 – 2013-04-30 12:58:43

+0

我忘了注入'$ http',我的坏!虽然不需要'$ compile'!如果您使用jQuery的'.html()'将HTML添加到网页,那么您做得不对,请使用单独的部分视图文件!我不确定你的'Info.xml'文件是怎么样的(我用静态数据来展示它的工作原理),但看到这个:http://jsfiddle.net/m23kX/ – 2013-04-30 20:00:57

+0

但我的想法是重用控制器在其他页面和每次手动注入html。我应该每次直接输入html吗? – 2013-05-07 06:14:01