サーバーから受け取った新しい html を埋め込むことはできますが、後でそれをモデルにバインドする必要があります。挿入して表示してから5秒後にコンパイルしても、htmlはモデルにバインドされません。
簡単に例を挙げてみましょう
function coolController($scope, $http, $log, $sce, $compile, $timeout){
$scope.message = {
error: 'Kernel panic! :)',
otherInfo: ''
}
$scope.form = $sce.trustAsHtml('<div></div>');
$scope.Init = function(){
$http({
method: 'GET',
url: helper.url('/form')
}).
success(function(data, status, headers, config) {
$scope.form = $sce.trustAsHtml(data);
$timeout(function(){
$compile(angular.element('#elemThatContainsTheNewHTML'))($scope);
}, 2500);
}).
error(function(data, status, headers, config) {
$scope.message.error = data;
});
}
}
htmlが
<div>
My cool error: {{ message.error }}
</div>
埋め込まれている場所は次のようになります。
<div ng-controller="coolController">
<h4>Hello???</h4>
<div ng-init="Init()">
<span class="alert-error" ng-if="errorMessage.length > 0">{{errorMessage}}</span>
</div>
<div id="elemThatContainsTheNewHTML" class="viewContent" ng-bind-html="form">
</div>
</div>
html は正しく埋め込まれていますが、モデルにバインドしたいと考えています。