一旦从服务器接收到新的 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 已正确嵌入,但我想将其绑定到模型。