html - 加载页面后加载html内容?

标签 html angularjs

我正在使用一个 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/

相关文章:

php - CSS/HTML 超链接从表单 CSS 中断

javascript - 为什么 Angular 根范围变量仅在两次单击事件按钮后才更新?

javascript - 滚动到具有固定高度的 div 内的元素

javascript - Angular 智能表中的过滤器,如何使其不搜索隐藏属性?

javascript - 在 Angular Bootstrap Modal 和父 Controller 之间共享范围

c# - 在 C# 中打开 HTML 文档

html - 浏览器如何计算 HTML5 <video> 的帧速率 (fps) 以实现准确的帧搜索?

javascript - 服务器响应后 CSS 被删除

javascript - 如何访问 Angular 2 对象的属性

javascript - Angular.js 动态获取数据