我正在使用一个 AngularJS 示例,我遇到一个问题是我加载了 html View ,然后在一个 div 之后是来自 Controller (数据库调用)的内容 html 数据,还有数据内容 html 标签,如 <hr> <images>
但它们是显示的,因为它不渲染 html,所以我想渲染该 html 部分。
我知道我的问题是来自数据库的数据延迟,因此它将作为计划文本显示。
我使用ng-bind-html
直到它们显示痛苦文本而不渲染 html 标签。
我有一个答案是延迟页面加载,这将成功工作,但这不是正确的方法,因为有时数据库数据可能需要很长时间,在这种情况下无法工作。
最佳答案
嗨,杰伊,您必须为您的示例制定指令,名称为“bind-unsafe-html”,传递您的 html 字符串或内容,然后它将重新渲染您的 html 内容。 例如。
app.directive('bindUnsafeHtml', ['$compile', function ($compile) {
return function(scope, element, attrs) {
console.log("in directive");
scope.$watch(
function(scope) {
// watch the 'bindUnsafeHtml' expression for changes
return scope.$eval(attrs.bindUnsafeHtml);
},
function(value) {
// when the 'bindUnsafeHtml' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
}]);
关于html - 加载页面后加载html内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22497072/