【发布时间】:2014-04-24 01:02:21
【问题描述】:
有没有人知道有一种方法可以让指令选择性地嵌入指令的主体?
例如,如果我有以下 div 和 myWrapper 的指令。
<div data-my-wrapper="foo">
<h1>Hello World</h1>
</div>
我想在我的指令中包含类似于此伪代码的代码。
if (foo.locked) {
// user a static template saying that the item is locked.
}
else {
// Tranclude the body content
}
有没有办法指示指令应该嵌入哪些内容?或者是否有推荐的方法来获得相当于 if/then/else 的方法,可以让我换出内容而不是指令所在的元素的主体?
【问题讨论】:
-
您可能正在寻找 transclude 链接函数(即指令
link函数的第五个参数)来访问已嵌入的内容以执行您需要做的事情。这是一个快速演示JSFiddle。 -
@miqid - 这就是我要找的!你能把这个作为答案让我接受吗?谢谢,我从来不知道控制器之后还有另一个参数!
标签: angularjs angularjs-directive