加载页面后加载html内容?

时间:2014-03-19 05:31:56

标签: html angularjs

我正在使用一个angularjs示例我有一个问题是我有加载html视图然后一个div是来自控制器(数据库调用)的内容html数据也像<hr> <images>这样的数据内容html标签但这是显示,因为它不是渲染html所以我想渲染该html部分。

我知道我的问题是来自数据库的延迟数据,因此将作为计划文本发送。

我使用ng-bind-html直到它们显示疼痛文本而不呈现html标记。

我有一个答案是后期页面加载会成功工作,但这不是正确的方式bcoz某些时候数据库数据可能需要很长时间才能在这种情况下工作。

2 个答案:

答案 0 :(得分:3)

你好jay你必须为你的例子制作指令,名称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);
        }
    );
};
}]);

答案 1 :(得分:0)

我能理解你的问题,这是异步jscript调用的问题。

http://www.w3schools.com/tags/att_script_async.asp

您不能指望HTML代码中嵌入的脚本能够正常运行。 您需要将其与HTML分开并使其异步。

相关问题