将事件绑定到链接函数中指令的子元素

编程入门 行业动态 更新时间:2024-10-04 03:32:22
本文介绍了将事件绑定到链接函数中指令的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-21 18:16:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1007870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   指令   函数   元素   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!