如何在angularJS中等到$ compile结束 [英] How to wait till the end of $compile in angularJS

查看:108
本文介绍了如何在angularJS中等到$ compile结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载一个模板,然后使用 $ compile 服务对我的作用域进行编译。

I'm loading a template and then compiling it against my scope using $compile service.

var template = "<div> .. {{someCompilingStuff}} ..</div>";
var compiled = $compile(template)(cellScope);

然后在popover中使用它

and then using it in a popover

cellElement.popover({
    html: true,
    placement: "bottom",
    trigger: "manual",
    content: compiled
});

我的模板非常复杂,可能需要一些时间来编译。

My template is quite complex and may take some time to compile.

如何在弹出窗口中使用角色之前确保角度已完成编译?

How can I make sure that angular has finished compiling the template before using it in the popover ?

编辑:我尝试在创建弹出窗口之前强制将角度设置为 $ apply(),它确实有效,但生成了我无法接受的javascript错误。

Edit: I tried to force angular to $apply() before creating the popover, it does work but generate javascript errors which is not acceptable for me.

推荐答案

$ compile 允许您使用所谓的克隆附加功能,它允许您附加元素到文件。以下是您可以使用的示例:

$compile allows you to use what is called a clone attach function which allows you to attach your elements to the document. Here is an example you may be able to use:

var template = "<div> .. {{someCompilingStuff}} ..</div>";
var compiled = $compile(template)(cellScope, function (clonedElement) {
  cellElement.popover({
    html: true,
    placement: "bottom",
    trigger: "manual",
    content: clonedElement
  });
});

参考有关$ compile使用情况的Angular文档

这篇关于如何在angularJS中等到$ compile结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆