将事件绑定到链接函数中指令的子元素 [英] Bind event to child element of directive in link function

查看:21
本文介绍了将事件绑定到链接函数中指令的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要将一个事件绑定到在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆