注入html时,Angularjs绑定不起作用

时间:2013-04-24 07:14:59

标签: javascript angularjs

我试着看documentation,但似乎我错过了一些东西。我正在尝试注入绑定到json的html。如果html被声明,它工作正常但是当我注入它时,尽管调用$ compile它不起作用。这是代码

<!DOCTYPE html>
<html ng-app>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js">        </script>
 <script src="todo.js"></script>
<script> 

 function TodoCtrl($scope)
  {
  $scope.todos = [{text:'LearnAngularJS'}, {text:'Unlearn Angular'},];
 }

 $(document).ready(function()
 {
 $('#div1').html(
$compile('<div ng-controller="TodoCtrl"><ul><li ng-repeat="todo in todos"    compile="text">{{todo.text}}<li><ul><div>')(scope));
 });

 </script>
</head>
<body>
<div ng-controller="TodoCtrl">
<ul>
 <li ng-repeat="todo in todos">
  {{todo.text}}
   <li>
  <ul>
   </div>

 <div id="div1">
 <div>

 </body>
 </html>

1 个答案:

答案 0 :(得分:4)

http://plnkr.co/edit/NQQBgQKBWEqKHxGwnI0h?p=preview

正如Ajay指出的那样,你必须将你的一个范围与模板相关联。