问题描述
好的,所以我创建了一个指令让我们说
Okay, so I've created a directive let's say
<calendar></calendar>
它按照我的预期呈现,所以一切正常。现在,我的问题是如何在插入DOM时(重新)渲染它?我不想一直在我的页面上使用它,我只想动态添加它并在我需要它时渲染(它是模块的一部分),让我们说,理想情况下我希望它看起来像
It gets rendered as I expected so everything works fine. Now, my question is how to (re)render it when inserted into DOM? I don't want to use it on my page all the time, I just want to dynamically add it and render just when I need it to (it's a part of a module), let's say, ideally I want it to appear like
$("body").append("<calendar></calendar>")
如何使用angularjs实现这一目标?
How can I achieve this with angularjs?
推荐答案
你需要在下面两行写下你想要将指令元素注入DOM的地方,不要忘记添加 $ document
& $ compile
依赖于您使用它的任何地方。
You need to write below two lines wherever you want to inject your directive element to DOM, don't forget to add $document
& $compile
dependency wherever you use it.
var template = '<calender></calender>',
body = $document.find('body');
body.append($compile(template)(scope));
这篇关于如何在插入DOM(angularjs)时重新编译指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!