You'll want to use $compile
to allow angular to dynamically compile this HTML template. $watch
will allow you to compile this template each time the AJAX-returned string changes.
HTML
<html ng-app="app">
<body>
<h1>AJAX-provided HTML into Angular-rendered template</h1>
<div ng-controller="MyController">
<div dynamic="html"></div>
</div>
</body>
</html>
javascript
var app = angular.module('app', []);
app.directive('dynamic', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamic, function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
});
function MyController($scope) {
$scope.updateHtml = function(html){
$scope.html = "";
}
}
To force the jQuery to update the Angular code, you could try calling the angular object for that DOM element (then accessing the controller for that object):
jQuery
$.ajax(href, {
success: function(data){
var element = angular.element($('#myElement'));
var scope = element.scope();
scope.$apply(function(){
scope.updateHtml(data);
});
}
});