问题描述
限时送ChatGPT账号..需要将一个事件绑定到在 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.
这里我想在用 ng-repeat 准备的 li
标签上绑定 click
事件.但是到时候,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.
有人可以帮忙吗.
推荐答案
我已经解决了与 angular $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屋!
更多推荐
[db:关键词]
发布评论