Can you use angular on dynamic fetched data? I have an existing application where most of the content is rendered by javascript after ajax-call. So I have this div that is filled by fetching data with ajax and then rendered by javascript. Well, in all these lot of html that is fetched by ajax, if I use ng-app on one of this elements, it doesnt seem to work. Doing the same thing with an html-element that has been fetched in the initial request (prior to the ajax-request) works.
So here is the code:
File: test.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" ng-app>
<head>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="ang_app.js"></script>
<script>
$( document ).ready(function() {
$.ajax({
url: "test2.php"
}).success(function(data) {
$('#content').html(data);
});
});
</script>
<body>
<h2>This is an app</h2>
<div ng-app ng-controller="StatsCtrl2">StatsCtrl2</div>
<div id="content"></div>
</body>
</html>
File test2.php:
<?php
echo '<div style="background-color:green;" ng-app ng-controller="StatsCtrl">Should be StatsCtrl</div>';
File ang_app.js:
function StatsCtrl($scope){
alert("function StatsCtrl");
}
function StatsCtrl2($scope){
alert("function StatsCtrl2");
}
Well, running this app I see the alert for "StatsCtrl2". But the alert inside "StatsCtrl" never shows app. Am I doing something wrong?