将事件绑定到链接函数中指令的子元素 [英] Bind event to child element of directive in link function
本文介绍了将事件绑定到链接函数中指令的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要将事件绑定到指令的子代,该子代在templateUrl中使用ng-repeat进行准备.
我试图将事件绑定到link
函数中,但子级尚未准备好.
Need to bind an event to directive's children which is prepared with ng-repeat in templateUrl.
I am trying to bind the event in link
function but the children are not yet prepared.
这是 plunker.
这里我想将click
事件绑定到用ng-repeat准备的li
标记上,但是到link
执行时,还没有准备好li
元素.
Here I want to bind click
event on li
tag which are prepared with ng-repeat.But by the time, the link
is executed, the li
elements are not yet prepared.
有人可以帮忙吗?
推荐答案
我已经解决了角度$ timeout的相同问题
I've resolved the same problem with the angular $timeout
link: function (scope, element) {
$timeout(function () {
element.on('click', '#Id', function () {
console.log('inside event handler of the first child)
})
})
}
通过在指令中注入$ timeout来尝试此操作
Try this by injecting $timeout in your directive
这篇关于将事件绑定到链接函数中指令的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文