Javascript 注入html时Angularjs绑定不起作用
我试着看了看,但似乎我错过了什么。我正在尝试注入绑定到json的html。如果声明了html,它就可以正常工作,但是当我调用$compile并注入它时,它就不工作了。这是密码Javascript 注入html时Angularjs绑定不起作用,javascript,angularjs,Javascript,Angularjs,我试着看了看,但似乎我错过了什么。我正在尝试注入绑定到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.jque
<!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>
函数TodoCtrl($scope)
{
$scope.todos=[{text:'LearnAngularJS'},{text:'Unlearn Angular'},];
}
$(文档).ready(函数()
{
$('#div1').html(
$compile('li ng repeat=“todo in todos”compile=“text”>{{{todo.text}})(范围));
});
-
{{todo.text}
-
正如Ajay所指出的,您必须将一个范围与模板相关联。您从何处获得(范围)值它将在您声明itI的位置上未定义。我试图遵循您的逻辑并设置范围,如:但它似乎缺少一些内容,againI试图清理并修复代码中的一些错误:Tosh-我很抱歉如此愚蠢!谢谢你启发我!