如何在ajax应用程序中加载Angularjs应用程序?
问题描述:
我试图加载Ajax页面内部的angularjs应用程序的一部分,我将它与控制器的工作,但有一个困难时期与模块如何在ajax应用程序中加载Angularjs应用程序?
页
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#bt").click(function(){
$.ajax({
url: "http://localhost/ajax/ng.php?p=1",
success: function(data) {
$("#game").html(data);
}
});
});
});
</script>
</head>
<body>
<input type="button" id="bt" value="poker"></input>
<div id="game">
</div>
</body>
</html>
angularjs应用PHP
<?php
if (isset($_GET["p"])){
header('Access-Control-Allow-Origin: *');
//echo "<script src=\"http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js\"></script><div ng-app=\"myApp\"><h1 ng-controller='MainCtrl'>{{text}}</h1></div>";
echo "<div ng-app><h1 ng-controller='MainCtrl'>{{text}}</h1></div><script src=\"http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js\"></script><script src=\"http://localhost/ajax/app.js\">";
}
?>
JS文件
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', ['$scope', function ($scope) {
$scope.text = 'Hello, Angular fanatic.';
}]);
每当我尝试在我的div中声明模块名称时,它不加载角度并仅显示{{text}}
答
您不会自动引导动态添加的html代码。不要使用NG-应用程序,使用
angular.bootstrap(element,["module"]
angular.bootstrap
你的HTML添加元素后必须被调用。另外,为什么不在整个文档中声明ng-app,并使用ng-include或ngRoute加载动态节?
我不是一个downvoter,但你的问题并不清楚。 '我已经使它与控制器一起工作了吗?你可以发布它,或为此创建一个小提琴http://jsfiddle.net/。您需要哪部分帮助? – Lekhnath
没有人可以帮助你了解更多信息,添加一些代码或更多细节。 – Prashobh
抱歉花花公子第一次问^^ – user3496808