角:如何获得由ID的元素从一个指令的前pression? [英] Angular: How to get an element by id with an expression from a directive?
问题描述
大件问题提前让我们打破这种下来。我尽力达成一个类似的行为就像天然<标签>
为<输入>
。 我不能窝他们,所以我用一种风格是这样的:
Bulky question ahead so let's break this down. I try to achieve a similar behavior like the native <label>
for <input>
. I can't nest them so I use a style like this:
<input type="checkbox" id="test" />
Some other content...
<label for="test">Lorem ipsum</label>
在我的情况下&LT;标签&gt;
是一个自定义指令和&LT;输入&GT;
是一个任意elment。它可能是这样的:
In my case <label>
is a custom directive and <input>
is an arbitrary elment. It could look like this:
<p id="test">Some element...</p>
Some other content...
<custom-directive for="test">My directive</custom-directive>
可悲的是我的 ID
是不是真的一成不变的。这是一个前pression。它可能是这样的:
Sadly my id
isn't really static. It is an expression. It could look like this:
<p id="test-{{ foo }}">Some element...</p>
Some other content...
<custom-directive for="test-{{ foo }}">My directive</custom-directive>
现在我的问题:在&LT;定制指令&GT;
我想在配置元素中的为
属性。比方说富
值为栏
在 $ rootScope $账套$ C>在
&LT;定制指令&GT;
我得到 foo的酒吧
。但是无论在哪里我试试吧(里面编译
,链接
,控制器
,使用或不使用优先级
等)的特定元素(在这里&LT; p&GT;
)的仍然测试 - {{富}}
在那个时候,所以我得到空
与的getElementById
。
Now to my problem: Inside <custom-directive>
I want to get the element configured in the for
attribute. Lets say foo
has the value "bar"
set in the $rootScope
inside <custom-directive>
I get foo-bar
. However no matter where I try it (inside compile
, link
, controller
, with or without a priority
, etc.) the specific element (here <p>
) has still test-{{ foo }}
at that time, so I get null
with getElementById
.
我怎样才能获得该元素的diretive外,它是否有一个 ID包含一个前pression
?
How can I get the element outside the diretive, if it has an id
containing an expression?
下面是一个例子: http://jsfiddle.net/cd6uooze/
(注:在我的现实世界的应用程序这个问题略有不同其实我可以的getElementById
从指令右边的元素,即使它的 ID
包含前pression。然而,这只能直到特定模板中的 $ templateCache
。我真的无法打破这个倒在的jsfiddle,但它从的jsfiddle的例子非常相似。)
(Note: In my real world app this problem is slightly different. I can actually getElementById
the right element from a directive even if it the id
contains an expression. However this works only until the specific template is inside the $templateCache
. I couldn't really break this down in the JSFiddle, but it the example from the JSFiddle is very similar.)
推荐答案
您需要编写您的getElementById code $超时内
You need to write your getElementById code inside $timeout
勾选此更新小提琴。
这篇关于角:如何获得由ID的元素从一个指令的前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!