我有以下2個文件,index.html文件使用角度ajax.html調用ajax調用,ajax.html文件包含一些角碼,我需要渲染的角度代碼ajax .html在index.html中,所以我可以將它替換爲div#listelement。我不能夠得到渲染代碼如何從角度ajax獲取渲染數據調用
的index.html(代碼)
<!DOCTYPE html>
<html lang="en" ng-app="myAppUpdate">
<head>
\t <meta charset="UTF-8">
\t <title>Angular Ajax HTML Render</title>
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
\t <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-controller="myControllerUpdate">
<div id="listelement">
\t <li>old data</li>
<li>old data v2</li>
</div> \t
<script>
var jq = $.noConflict();
var myAppUpdate = angular.module('myAppUpdate',[]);
console.log('outside');
// var myApp = angular.module('myApp',['ui.bootstrap']);
myAppUpdate.controller('myControllerUpdate', function ($scope,$http,$compile) {
\t console.log('inside');
$http.get('ajax.html')
\t .success(function (data) {
console.log(data);
console.log('working');
var html = jq('#listelement').html(data);
\t \t \t \t $compile(html)($scope);
// var newstuff = $formBlock.html(response);
\t // $compile(newstuff)(scope);
});
});
</script>
</body>
</html>
ajax.html(代碼)
<div data-ng-controller="myController">
\t <li ng-repeat="x in data">
\t \t {{x}}
\t </li>
</div>
\t
<script>
var myApp = angular.module('myAppUpdate',[])
myApp.controller('myController',function ($scope) {
\t $scope.data = ['1','2','3'];
});
</script>
</body>
</html>
您應該使用路由以獲得更好的偏好。 – Jigar7521